ststeiger / PdfSharpCore

Port of the PdfSharp library to .NET Core - largely removed GDI+ (only missing GetFontData - which can be replaced with freetype2)
Other
1.08k stars 237 forks source link

Cannot open generated PDF using Adobe Reader #66

Open MazurchakVova opened 4 years ago

ststeiger commented 4 years ago

I believe you, but please add additional details. Otherwise I can't do anything about it.

MazurchakVova commented 4 years ago

For example code https://github.com/MazurchakVova/ConvertHtmlToPDF

test.pdf

MrMM1981 commented 4 years ago

Solved the problem by implementing custom event handler when loading images `... private void OnImageLoad(object sender, HtmlImageLoadEventArgs e) { ImageSource.ImageSourceImpl = new CustomImageSource();

        // Base64 encoded images needs to be converted manually
        if (e.Src.StartsWith("data:image/", StringComparison.InvariantCultureIgnoreCase))
        {
            var base64Data = e.Src.Substring(e.Src.IndexOf(",", StringComparison.InvariantCulture) + 1);
            var imageBytes = Convert.FromBase64String(base64Data);

            var stream = new MemoryStream(imageBytes);

            var imgStream = XImage.FromStream(() => stream);

            e.Callback(imgStream);
        }
        // Images with a path source
        else
        {
            var templateImages = Path.Combine(TemplateRoot, "Images");
            var imageFile = Path.Combine(templateImages, e.Src);

            if (File.Exists(imageFile))
            {
                var imageBytes = File.ReadAllBytes(imageFile);
                var stream = new MemoryStream(imageBytes);

                var imgStream = XImage.FromStream(() => stream);

                e.Callback(imgStream);
            }
        }
    }

....`

and implementation for CustomImageSource

` using MigraDocCore.DocumentObjectModel.MigraDoc.DocumentObjectModel.Shapes; using SixLabors.ImageSharp; using SixLabors.ImageSharp.Formats.Bmp; using SixLabors.ImageSharp.Formats.Jpeg; using SixLabors.ImageSharp.PixelFormats;

public class CustomImageSource : ImageSource where TPixel : struct, IPixel {
protected override IImageSource FromBinaryImpl(string name, Func<byte[]> imageSource, int? quality = 75) { return new CustomImageSourceImplementation(name, () => Image.Load(imageSource.Invoke()), Quality); }

    protected override IImageSource FromFileImpl(string path, int? quality = 75)
    {
        return new CustomImageSourceImplementation<TPixel>(path, () => Image.Load<TPixel>(path), Quality);
    }

    protected override IImageSource FromStreamImpl(string name, Func<Stream> imageStream, int? quality = 75)
    {
        return new CustomImageSourceImplementation<TPixel>(name, () =>
        {
            using (var stream = imageStream.Invoke())
            {
                return Image.Load<TPixel>(stream);
            }
        }, Quality);
    }

    private class CustomImageSourceImplementation<TPixel2> : IImageSource where TPixel2 : struct, IPixel<TPixel2>
    {
        private readonly Func<Image<TPixel2>> _getImage;
        private readonly int _quality;

        private Image<TPixel2> _image;
        private Image<TPixel2> Image => _image ?? (_image = _getImage.Invoke());

        public int Width => Image.Width;
        public int Height => Image.Height;
        public string Name { get; }

        public bool Transparent => _image?.PixelType?.BitsPerPixel == 32;

        public void SaveAsPdfBitmap(MemoryStream ms)
        {
            Enum.TryParse<BmpBitsPerPixel>($"{_image?.PixelType?.BitsPerPixel}", out var _bpp);
            Image.SaveAsBmp(ms, new BmpEncoder { BitsPerPixel = _bpp});
        }

        public CustomImageSourceImplementation(string name, Func<Image<TPixel2>> getImage, int quality)
        {
            Name = name;
            _getImage = getImage;
            _quality = quality;
        }

        public void SaveAsJpeg(MemoryStream ms)
        {
            Image.SaveAsJpeg(ms, new JpegEncoder { Quality = _quality });
        }

        public void Dispose()
        {
            Image.Dispose();
        }
    }
}

}`

Hope it helps others who run into this problem

ststeiger commented 4 years ago

Interesting, obviously a PDF generated from a HTML source. Wondering now what purpose that image is, as I see only an empty page with the Adobe PDF-viewer.

MrMM1981 commented 4 years ago

Hi Stefan, for me it was a problem when adding images from a template. It had to do with transparency of those images.

Thank you for your reply!

Verstuurd vanaf mijn iPhone

Op 15 mei 2020 om 14:25 heeft Stefan Steiger notifications@github.com het volgende geschreven:

 Interesting, obviously a PDF generated from a HTML source. Wondering now what purpose that image is, as I see only an empty page with the Adobe PDF-viewer.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.