I have a working solution for getting PDF previews with the MBS plugin:
dim PreviewPic as Picture
try
dim thePDF as new PDFDocumentMBS(MimeData)
if thePDF <> nil then
dim thePDFPage as PDFPageMBS = thePDF.pageAtIndex(0)
dim theColor as NSColorMBS = NSColorMBS.whiteColor
if thePDFPage <> nil then
dim theImage as NSImageMBS = thePDFPage.Render(DPI, 0, theColor)
if theImage <> nil then PreviewPic = theImage.CopyPicture
end if
end if
Return PreviewPic
Catch err as NSExceptionMBS
'no pages
end try
This works fine for most of the PDFs. Except when the PDF is odd. I have one PDF which is fine on High Sierra and makes my app and Preview crash on Mojave. I have a fallback solution using the sips command
dim theShell as new Shell
dim theCommand as String = "sips -s format png " + thePDFFile.ShellPath + " --out " + theResultFolderitem.ShellPath
theShell.Execute(theCommand)
which unfortunately is much slower than the MBS plugin. I’ve now got a customer who sees crashes from sips even on High Sierra.
I have looked for a replacement library to get the PDF previews. I don’t like to whine about prices but DynaPDF is way too expensive right now. I have looked around but only found either GPL solutions or enterprisey ones (“we are too expensive to put our prices on the website”). Does anyone know about a solution that works for macOS and doesn’t cost an arm and a leg?
@Beatrix Willius Well I agree that most of what we see from Quicklook is slow, however that does not mean that every Quicklook function is slow. I suspect that the Finder uses Quicklook previews and they don’t seem particularly slow to me, even in asynchronous mode.
And on the bright side, if an XPC helper crashes during the process, it will not impact your application.
dim c as new CGImageSourceMBS(f)
dim img as CGImageMBS
dim d as new Dictionary
d.Value(c.kCGImageSourceThumbnailMaxPixelSize)=YourSize
d.Value(c.kCGImageSourceCreateThumbnailFromImageAlways)=True
d.Value(c.kCGImageSourceShouldAllowFloat)=True
d.Value(c.kCGImageSourceCreateThumbnailWithTransform)=True
img = c.CreateThumbnailAtIndex(0, d)
p = img.Picture
I don’t know if you can eliminate the possibility of any PDF engine ever crashing on an “odd” PDF document (i.e. a PDF with errors).
Perhaps you should spin the PDF-to-image code out to a helper app. If it crashes, your entire app doesn’t come down and you can inform the user that they may have a damaged PDF.
but PDF renderer WILL crash (including Preview, and Adobe Reader) if the PDF file itself is corrupt. Sometimes the stand-alone readers will “recover” and fail gracefully… but I would not expect that from command line tools such as SIPS where they expect to fail back to the command line (ie. crash).
I ran across this many times when writing my gPDF package, when testing code that wasn’t producing a valid PDF as expected
Got some data from the customer. Holy guacamole. Whatever is in the PDF makes my iMac crawl when the PDF is rendered. I quit Xojo and the debug app still was running.
Using CGImageSourceMBS for the customers PDF works fine. However, as expected CGImageSourceMBS still crashes on Mojave with another corrupt PDF that I have because macOS only uses the same PDF library everywhere.
@Dave S : below is the beginning of the crash log from sips. I was very very surprised to see something like this:
Process: sips [6210]
Path: /usr/bin/sips
Identifier: sips
Version: 284
Code Type: X86-64 (Native)
Parent Process: Mail Archiver X [3326]
Responsible: sips [6210]
User ID: 502
Date/Time: 2019-04-22 14:29:43.968 +0200
OS Version: Mac OS X 10.13.6 (17G6030)
Report Version: 12
Anonymous UUID: A1B78A3D-87C9-1685-791E-3EA776CDCF5D