[Wontdo] Wrap JPEG as-is into PDF markup? Export JPEG as PDF without recompressing?

Having tech problems? Post them here and do not worry, there is always a solution for you.
Post Reply
porg
Posts: 554
Joined: Tue Apr 05, 2022 1:04 pm
Country: Austria

[Wontdo] Wrap JPEG as-is into PDF markup? Export JPEG as PDF without recompressing?

Post by porg »

I have a JPEG with resolution info (600 DPI as it came from the scanner)

• I'd like to export this as a PDF, but without recompression.
• Basically just creating an empty PDF, with exactly the dimensions as the JPEG (true to the original scan)
• And just wrap / place the JPEG file resource as-is into into the PDF with the correct markup (postscript syntax), so that it fills the PDF 100%.
• And the PDF has the correct metadata (dimensions & resolution).
So that someone can then print this easily true to scale.

I looked into the manual and tried everything in the PDF file format dialog, but found nothing adequate there:

GraphicConverter PDF file format options.png
GraphicConverter PDF file format options.png (103.24 KiB) Viewed 1236 times
User avatar
forum_adm
Site Admin
Posts: 1814
Joined: Fri Dec 23, 2016 9:41 am
Location: Germany
Country: Germany
Contact:

Re: Wrap JPEG as-is into PDF markup? Export JPEG as PDF without recompressing?

Post by forum_adm »

This is not possible wit the used PDF SDK or Apple Core Image.
porg
Posts: 554
Joined: Tue Apr 05, 2022 1:04 pm
Country: Austria

Re: Wrap JPEG as-is into PDF markup? Export JPEG as PDF without recompressing?

Post by porg »

1) Thanks for the answer.

2) I found a great StackOverflow Q&A exactly on topic: Storing jpg images into a pdf file in a "lossless" way
with the answers section currently featuring: 1 CLI/GUI app, 2 CLIs and 1 GUI app for exactly this purpose.

3) I went with img2pdf which worked like a charm!

Its homepage perfectly summed up my needs:
Lossless conversion of raster images to PDF. You should use img2pdf if your priorities are (in this order):

1. always lossless: the image embedded in the PDF will always have the exact same color information for every pixel as the input
2. small: if possible, the difference in filesize between the input image and the output PDF will only be the overhead of the PDF container itself
3. fast: if possible, the input image is just pasted into the PDF document as-is without any CPU hungry re-encoding of the pixel data

Conventional conversion software (like ImageMagick) would either:
1. not be lossless because lossy re-encoding to JPEG
2. not be small because using wasteful flate encoding of raw pixel data
3. not be fast because input data gets re-encoded
Installation & usage was super easy, worked all correct at my very first command invocation!

Code: Select all

# Installation is just a single line with pipx, which I already had
$ pipx img2pdf

# Usage — Really simply & the defaults already perfectly matched my needs

$ cd ~/Pictures/Scans/img2pdf 
$ img2pdf --nodate test-dpi150-s20-q60.jpg > test-dpi150-s20-q60.pdf
$ img2pdf --nodate test-dpi600-s20-q60.jpg > test-dpi600-s20-q60.pdf

# Remarks
# - By default creates a PDF with exactly the image dimensions
# - and the color space of the input image
# --nodate suppresses the timestamp within the PDF to get the same deterministic outcome
#   also when repeating the conversion with the same input file.
The PDFs had just an extra of ca 500 bytes (the PDF markup) and both JPEG input sources files (with resolution in their metadata) resulted in a PDF with the correct dimensions (viewed in Preview.app).

And there are plenty of options for custom sizes/positioning, overriding color spaces, how to handle multi frame image formats (use only first, or 1 page per each frame), etc. Easy for beginners, pro options if needed!

4) If that feature request every comes up for GraphicConverter (from customers ready to pay for that) this is possibly easy to integrate:

a) Is available as a Library too.
b) License is GNU Lesser General Public License. Not sure what this means in practise for embedding it into shareware, but as a professional developer you certainly know.
Post Reply