Need help to convert a pdf to jpg on my mac

I am using the built-in Preview app to convert pdf to jpg on my macbook pro. After conversion, I can see one jpg file in the output folder and all pages of pdf file are included in the jpg file when viewing with Preview on this Mac.


However, when I sent the jpg file to my wife and open it on a PC, only the first page of pdf file is visible. The rest pages are gone. Is this normal and how can I bulk convert pdf to jpg on Mac (one page per jpg file)?


MacBook Pro (2017 – 2020)

Posted on Nov 7, 2023 7:25 PM

Reply
Question marked as Top-ranking reply

Posted on Nov 8, 2023 6:55 AM

I have an AppleScript/Objective-C solution that prompts you for the PDF, and then another prompt for the output image format (e.g. JPG, PNG, TIF, JP2). It then creates a new folder bearing the basename of the PDF and into it are written the Images as basename_001.jpg ... basename_nnn.jpg. It is very quick at doing this despite the amount of code below.


A work in progress is a third-prompt requesting the page numbers to convert. This can be 1,2,3-9 and it will expand that as a sequential range of pages. It is not however fully tested (not included) where you specify the page numbers or range, and the corresponding pages will be converted.


One launches Apple's Script Editor and copy/pastes the following content into it. Click compile and then run.


-- pdf2image.applescript
-- this is the PDF to selected image version

-- attribution: https://www.macscripter.net/t/pdf-to-tiff-script/74847

use framework "Cocoa"
use framework "PDFKit"
use AppleScript version "2.4" -- Yosemite or later
use scripting additions

property ca : current application
property bitmapList : {"JPG", "PNG", "TIF", "JP2"}
property resolution : 300.0

set theKeys to bitmapList
set theValues to {ca's NSBitmapImageFileTypeJPEG, ca's NSBitmapImageFileTypePNG, ca's NSBitmapImageFileTypeTIFF, ca's NSBitmapImageFileTypeJPEG2000}
set dictx to ca's NSDictionary's alloc()'s initWithObjects:theValues forKeys:theKeys

set thePDF to POSIX path of (choose file of type "com.adobe.pdf" default location (path to desktop)) as text
set imageType to (choose from list bitmapList with title "Select image format" without multiple selections allowed and empty selection allowed) as text
if imageType is false then return

set imgEnum to dictx's objectForKey:imageType
set imgExt to ((ca's NSString's stringWithString:imageType)'s localizedLowercaseString) as text
my pdf2jpg(thePDF, imgEnum, imgExt, resolution)
return

on pdf2jpg(apdf, imgEnum, imgExt, aresolution)
	set pdfURL to ca's NSURL's fileURLWithPath:apdf
	set pdf to ca's PDFDocument's alloc()'s initWithURL:pdfURL
	set pdfFolder to pdfURL's URLByDeletingLastPathComponent
	set pdfFileName to (pdfURL's URLByDeletingPathExtension())'s lastPathComponent()
	set newFolder to pdfFolder's URLByAppendingPathComponent:pdfFileName
	
	repeat with i from 1 to pdf's pageCount()
		
		set aPage to (pdf's pageAtIndex:(i - 1))
		set pageSize to (aPage's boundsForBox:(ca's kPDFDisplayBoxMediaBox))
		set pageWidth to ca's NSWidth(pageSize)
		set pageHeight to ca's NSHeight(pageSize)
		set pixelWidth to (pageWidth * aresolution / 72) div 1
		set pixelHeight to (pageHeight * aresolution / 72) div 1
		set theImageRep to (ca's NSPDFImageRep's imageRepWithData:(aPage's dataRepresentation()))
		set newImageRep to (ca's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:pixelWidth pixelsHigh:pixelHeight bitsPerSample:8 samplesPerPixel:4 hasAlpha:yes isPlanar:false colorSpaceName:(current application's NSDeviceRGBColorSpace) bytesPerRow:0 bitsPerPixel:32)
		
		ca's NSGraphicsContext's saveGraphicsState()
		(ca's NSGraphicsContext's setCurrentContext:(ca's NSGraphicsContext's graphicsContextWithBitmapImageRep:newImageRep))
		ca's NSColor's whiteColor()'s |set|()
		ca's NSRectFill({origin:{x:0, y:0}, |size|:{width:pixelWidth, height:pixelHeight}})
		(theImageRep's drawInRect:{origin:{x:0, y:0}, |size|:{width:pixelWidth, height:pixelHeight}} fromRect:(ca's NSZeroRect) operation:(ca's NSCompositeSourceOver) fraction:1.0 respectFlipped:false hints:(missing value))
		ca's NSGraphicsContext's restoreGraphicsState()
		-- make 300 dpi pages instead of default 72 dpi 612x792 default if this is commented
		(newImageRep's setSize:{pageWidth, pageHeight}) -- if desired
		-- zero pad counter to three digits
		set paddedSequence to (ca's NSString's stringWithFormat:("_%02d" & i))
		set imageFileName to (pdfFileName's stringByAppendingString:paddedSequence)
		
		set theData to (newImageRep's representationUsingType:imgEnum |properties|:{NSImageCompressionFactor:0.8}) -- 0.0 is maximum compresssion and 1.0 is no compression 
		set fileMgr to ca's NSFileManager's defaultManager()
		
		-- use the base filename as the folder to create at the current path
		-- will return true if folder already exists
		set {done, theError} to (fileMgr's createDirectoryAtURL:newFolder withIntermediateDirectories:true attributes:(missing value) |error|:(reference))
		if done as boolean then
			set theImage to ((newFolder's URLByAppendingPathComponent:imageFileName)'s URLByAppendingPathExtension:imgExt)
			(theData's writeToURL:theImage atomically:true)
		end if
	end repeat
	return
end pdf2jpg



Similar questions

29 replies

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Need help to convert a pdf to jpg on my mac

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.