Allow Lossy PDF #54

Closed
opened 2021-04-25 19:58:17 +00:00 by josch · 0 comments
Owner

By Avner on 2018-12-30T15:39:56.173Z

First thing, Thanks for making this lib!

My use case is pretty simple, I get various png/jpegs/pdf from multiple resources I can not control and need to display them on the web.
For an easy client side behavior, we have decided to convert all to PDFs.

I do however get every once in a while this error:

Image contains transparency which cannot be retained in PDF.
img2pdf will not perform a lossy operation.
You can remove the alpha channel using imagemagick:
$ convert input.png -background white -alpha remove -alpha off output.png

Which is pretty descriptive and straight forward, I wonder however if it will be possible to add a flag to img2pdf that will basically allow a "lossy operation", In my case, I'd rather show something, than nothing.
Is this at all possible? or is there some inherent limitation I am missing?

Thoughts?


By josch on 2018-12-30T22:00:59.698Z


What stops you from removing the alpha channel from your input before passing them to img2pdf?

Why should img2pdf do the task and not your own code instead?


By Avner on 2018-12-31T10:08:39.168Z


@josch
For me, that would mean installing imagemagick and as a result, unneeded complexity with server and local dev setup.
Since this is a specific protection in img2pdf, I was thinking that it would be harmful to simply add a "force" flag, or "--allow-lossy".

I'd really just rather have this all in one. Maybe I am missing something, but I don't see why img2pdf should have this premise of "lossless", as in other converter, I'd expect this to have a default (lossless) but allow an opt-in override.


By josch on 2018-12-31T16:34:19.346Z


No, you don't have to install imagemagick. You can do the same with PIL which you must already have installed because img2pdf requires PIL. So removing the alpha channel is only a single line of code away:

img.convert('RGB')

The whole point of img2pdf is lossless conversion. If you want lossy conversion, then there exist tons of other tools already that are doing this task for you just fine (see README.md). Every additional command line option makes a tool harder to use because there is more documentation to read (not only more options have to be documented but also text has to be added like "yes, it is lossless except in these and those conditions..."). Every additional command line option makes the code more fragile because more conditions have to be handled and more unit tests have to be written. In contrast to all these downsides, the only thing that you have to do is to add a single line of code. So given how it is basically free for you to do this conversion, the additional complexity a "--allow-lossy" option would mean to img2pdf is not justified.

img2pdf aims to do one thing and this one thing well. That one thing is lossless conversion of raster images into a PDF container. If you want something else, then either add a single line into your code (see above) or use a different tool.


By Avner on 2018-12-31T16:53:14.165Z


@josch
This is perfect, thanks for the time you took to reply.
I guess the instruction to go with imagemagick in the exception warning me.
But I can totally see how this is a good generic recommendation.

Again, thanks for the guidance and patience here.


By Avner on 2018-12-31T16:53:14.464Z


Status changed to closed

*By Avner on 2018-12-30T15:39:56.173Z* First thing, Thanks for making this lib! My use case is pretty simple, I get various png/jpegs/pdf from multiple resources I can not control and need to display them on the web. For an easy client side behavior, we have decided to convert all to PDFs. I do however get every once in a while this error: > Image contains transparency which cannot be retained in PDF. > img2pdf will not perform a lossy operation. > You can remove the alpha channel using imagemagick: > $ convert input.png -background white -alpha remove -alpha off output.png Which is pretty descriptive and straight forward, I wonder however if it will be possible to add a flag to img2pdf that will basically allow a "lossy operation", In my case, I'd rather show something, than nothing. Is this at all possible? or is there some inherent limitation I am missing? Thoughts? --- *By josch on 2018-12-30T22:00:59.698Z* --- What stops you from removing the alpha channel from your input before passing them to img2pdf? Why should img2pdf do the task and not your own code instead? --- *By Avner on 2018-12-31T10:08:39.168Z* --- @josch For me, that would mean installing imagemagick and as a result, unneeded complexity with server and local dev setup. Since this is a specific protection in img2pdf, I was thinking that it would be harmful to simply add a "force" flag, or "--allow-lossy". I'd really just rather have this all in one. Maybe I am missing something, but I don't see why img2pdf should have this premise of "lossless", as in other converter, I'd expect this to have a default (lossless) but allow an opt-in override. --- *By josch on 2018-12-31T16:34:19.346Z* --- No, you don't have to install imagemagick. You can do the same with PIL which you must already have installed because img2pdf requires PIL. So removing the alpha channel is only a single line of code away: img.convert('RGB') The **whole point** of img2pdf is lossless conversion. If you want lossy conversion, then there exist tons of other tools already that are doing this task for you just fine (see README.md). Every additional command line option makes a tool harder to use because there is more documentation to read (not only more options have to be documented but also text has to be added like "yes, it is lossless *except* in these and those conditions..."). Every additional command line option makes the code more fragile because more conditions have to be handled and more unit tests have to be written. In contrast to all these downsides, the only thing that *you* have to do is to add a single line of code. So given how it is basically free for you to do this conversion, the additional complexity a "--allow-lossy" option would mean to img2pdf is not justified. img2pdf aims to do one thing and this one thing well. That one thing is lossless conversion of raster images into a PDF container. If you want something else, then either add a single line into your code (see above) or use a different tool. --- *By Avner on 2018-12-31T16:53:14.165Z* --- @josch This is perfect, thanks for the time you took to reply. I guess the instruction to go with imagemagick in the exception warning me. But I can totally see how this is a good generic recommendation. Again, thanks for the guidance and patience here. --- *By Avner on 2018-12-31T16:53:14.464Z* --- Status changed to closed
josch closed this issue 2021-04-25 19:58:18 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: josch/img2pdf#54
No description provided.