unidoc / unipdf

Golang PDF library for creating and processing PDF files (pure go)
https://unidoc.io
Other
2.47k stars 250 forks source link

[QUESTION] Quality of PDF to image #478

Closed polderudo closed 2 years ago

polderudo commented 2 years ago

We'r trying to render a image from a PDF (https://github.com/unidoc/unipdf-examples/blob/master/render/pdf_image_render.go) which works much faster now with the latest verion. Took seconds before and now only ms. Thanks for this.

But we are still having some problems with the quality of the output (jpg or png doesn't matter). I'm attaching the PDF and the created jpg here 1.pdf 1_1

We set device.OutputWidth = 600 but you can see, that the barcode is still blurred. Is there any way to generate the jpg in a better quality? Even a OutputWidth of 1200 does not realy help.

gunnsth commented 2 years ago

Hi @polderudo The barcode seems like it not a vector graphic, but a rasterized image, when I zoom in on the PDF, it does not appear all that crisp either, did you compare those?

polderudo commented 2 years ago

Hi Gun, thanks for the answer. Right now we generate the barcode with the wonderfull lib github.com/boombuler/barcode This gives us an image.Image that we add to the PDF with NewImageFromGoImage. What kind of vector graphic would have in mind? SVG? I didn't find a way to add a SVG to the PDF generation though. And yes, i am also wondering, why the barcode is shown nicely in the PDF, even if zooming very deep, but blured in the generate image.

adrg commented 2 years ago

Hi @polderudo.

Could you provide more details regarding how you generate and embed the bar code? Maybe a source code sample if at all possible. The image embedded in the PDF file you provided is only 90 pixels wide and 1 pixel tall. That's really small and it results in the image being blurred in the rendered file.

14 0 obj
<</Filter /FlateDecode/Type /XObject/Subtype /Image/Width 90/Height 1/BitsPerComponent 8/ColorSpace /DeviceRGB/Length 53>>

Adobe Reader seems to do some clever scaling for these 1 row images. That's why the quality is preserved even when zooming in. You can see that the other images in the PDF file you provided get increasingly "pixelated" as you zoom in, but not the bar code one.

I made a quick test using https://github.com/unidoc/unipdf-examples/blob/master/barcode/pdf_add_barcode.go and generated a PDF with a bar code in it (500x500 in size). The example is also using github.com/boombuler/barcode. That one renders ok because the image is not that small.

Here's the generated PDF file and here's how it's rendered: image.

polderudo commented 2 years ago

Hello Adrian, thanks for the hint. We were not scaling the image we get from Encode. With scaling everything is ok now. Multumesc :)