unidoc / unipdf

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

[BUG] image to pdf, size increase too much #447

Closed tangximing closed 2 years ago

tangximing commented 3 years ago

Description

when converting image to pdf, the size increase too much

Expected Behavior

when converting image to pdf, the size is almost the same

Actual Behavior

when converting image in size 3m, the size of the pdf is almost 25m

gunnsth commented 3 years ago

@tangximing Please include a full repro, i.e. code and files so we can reproduce the issue.

plar commented 3 years ago

We have the same issue. I just created a simple project which reproduce the issue.

https://github.com/plar/unidoc_image_bug

gunnsth commented 3 years ago

The default encoding filter, FlateDecode is not very efficient for images. Please use DCT (JPEG) instead which produces much smaller files. The example https://github.com/unidoc/unipdf-examples/blob/master/image/pdf_add_image_to_page.go has been updated to do this.

gunnsth commented 2 years ago

@plar Did you get a chance to try this with a different image encoding filter?

plar commented 2 years ago

DCT works fine. As a feature request it would be really nice if the library itself determines the type of encoding filter depending on the type of image.

gunnsth commented 2 years ago

@plar Good point. In most cases DCT probably fine for color images. If gray scale, then 8 bit DCT. If monochrome then JBIG2 etc. Will create a ticket for this, may need some research and experimentation to determine what gives best results. In the meantime explicitly setting DCT is a good practice. Will close this issue for now. Thanks.