I want to save a PNG image (created with screencapture) in a PDF with the image being saved with lossless (FlateDecode) compression. I can do this by opening the PNG image in Preview and saving it as a PDF. However, the following script saves it with lossy (DCTDecode) compression. I wondered if anyone might know the reason for that or how to change this?
BTW, I concluded that the image is saved in the PDF with DCTDecode compression based on the Xee3 image viewer and my inspection of the objects in the PDF. I donāt have Adobe Acrobat, which would be more definitive, but I think my conclusion is correct. Also, I tried different TIFF compression formats including none but that didnāt make a difference.
I know this is a bit convoluted, but I thought I would ask just in case. Thanks for reading my request.
use framework "AppKit"
use framework "Foundation"
use framework "Quartz"
use scripting additions
set imageFile to POSIX path of (path to desktop) & "Source.png"
set imageFile to current application's |NSURL|'s fileURLWithPath:imageFile
set pdfFile to POSIX path of (path to desktop) & "Test.pdf"
set imageRep to (current application's NSBitmapImageRep's imageRepWithContentsOfURL:imageFile)
set theData to (imageRep's representationUsingType:(current application's NSTIFFFileType) |properties|:{NSTIFFCompression:(current application's NSTIFFCompressionLZW)})
set theImage to (current application's NSImage's alloc()'s initWithData:theData)
set thePDFPage to (current application's PDFPage's alloc's initWithImage:theImage)
set pdfDoc to current application's PDFDocument's new()
(pdfDoc's insertPage:thePDFPage atIndex:0)
pdfDoc's writeToFile:pdfFile
zevrix. Thanks for reading my post and for the suggestion.
I did try no compression by using NSTIFFCompressionNone and by setting the properties to missing value. In both instances, the PDF was created but the PNG was saved in the PDF with DCTDecode compression. Another thing I tried was NSPNGFileType but that also didnāt work.
Some of these options are poorly documented (if at all). There might be an option to specify image compression settings. However, even if such option does exist, you might no be able to find it anywhere anyway. Try to google it.
Create a Quartz Filter in ColorSync Utility with this setting:
To see if this filter makes any difference to your particular PDF with DCTDecode compression, try to resave it from Preview while applying this filter.
If it does indeed affect the compression type, then you can use this Quartz Filter in the aforementioned withOptions: dictionary:
Out of curiosity, how are you determining that it is using the DCT compression?
I took a screen shot and created a PDF using your script. I then extracted its embedded image and looked at it using xee and it specifies that the compression scheme is ādeflateā. Looking at the image with imagemagickās identify shows āzipā as the compression scheme, which presumably correlates to deflate.
One of the other PDF compression schemes has a lossless setting although Iām not sure how you would access it ā JPXDecode.
Also, have you looked at the PDF/X format? It is designed for prepress and lossy compression is not an option with it. Again, not sure how you access it for general use without Acrobat (or other specialized software).
Thatās version 1 of the standard, released two decades ago. I believe it was removed in the next release. They may have added some form of compression in the most recent release as well but Iām not certain ā there are some things I donāt need to know.
I think there are other tools that can generate PDF/X but like Adobeās tools, Iām not sure that theyāre intended for casual users. Ghostscript might have the wherewithal (as well as any products that are built upon it) but thatās another research project.
@Mockman. I made the compression determination primarily by viewing the objects inside of the PDF. The object for the PNG image in the Preview-created PDF was:
I also viewed the PDFs in Xee3 and object 5 in the Preview-created PDF is shown as a TIFF and object 5 in the script-created PDF is shown as a JPEG. They are not actually a JPEG or TIFF, and I assume this simply reflects the type of compression used.
Adobe Acrobat Pro apparently has (or used to have) an object viewer identified as āBrowse Internal PDF Structureā accessible from the Advanced menu, and this would be determinative, but I donāt have that app. This is discussed here
The results you are getting when extracting the PNG image from the PDFs is interesting. I only have a basic understanding of the operation of PDFs and perhaps I just donāt understand what is happening here.
Lossy compression is the default in all PDF/X standards. PDF/X-4 is the latest.
I worked in prepress for couple decades. While the compression is in theory ālossyā, under Maximum setting thereās no loss detectable by human eye.