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
Question marked as Top-ranking reply

Nov 8, 2023 6:55 AM in response to Kycvincent

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



Nov 8, 2023 11:38 PM in response to Kycvincent

I saw a few folks suggesting Adobe Acrobat Reader in here and said it is free. This is not true in my case (running macOS Monterey 12.6.3). When you hit the Convert button (Export a PDF -> image format -> JPEG), it opens a new web page and asks you to sign up a subscription with Adobe Acrobat service, starting at $29.99 per month.


For the sole purpose of PDF conversion, it is more suitable with other options, including the free and paid ones. The following post introduces 5 different ways to convert PDF to JPG on Mac. You can check it out:


https://www.uubyte.com/blog/5-best-ways-to-convert-pdf-to-jpg-on-mac-ventura/


Nov 8, 2023 8:15 AM in response to dialabrain

And I tried exporting a PDF to jpeg, or Save As to a folder as image .jpg — both prior to my post on this subject and just a moment ago with the same Adobe pay us dialog prompts. Acrobat Reader DC is up to date.


If there is a different approach that works for you, please let me know. The following fails with a pay option when clicking Export to JPEG:


Nov 9, 2023 7:21 AM in response to Luis Sequeira1

This is fine if you like 72 dpi images from the PDF pages regardless of the DPI setting. I have a nested looping version of your shortcut so that n-tuple PDFs can be selected in the Finder, and each PDF page is then converted to JPG.


The ASOC code that I posted earlier gives one a choice of output image type and as written, produces real 300 dpi images (except for some reason, the JP2 are 72 dpi).


I would have posted a shorter, faster Swift solution that actually uses concurrent page processing to desired image and DPI settings — but Apple does not bundle Swift with the operating system and I don't like asking users to install the Xcode command line tools to use it.

Nov 9, 2023 7:43 AM in response to VikingOSX

The shortcut that I posted allowed for selected the DPI - it was not shown in the screenshot, but I actually had it set to 300 dpi for JPEG, and it worked. Some of these actions have a "Show more" button that allows for extra settings.


This is not dismissing your sophisticated Applescript in any way.

It is great and very much appreciated.


I just wanted to remind people that some very quick automation can be achieved without much in the way of programming - and more detailed and sophisticated can be achieved, as you did, too.



Nov 9, 2023 8:44 AM in response to dialabrain

Although I have no other Adobe products installed than Acrobat Reader DC, I have had an Adobe account for well over a decade and when I sign in to it via Adobe Acrobat Reader DC, I still get a pay wall for PDF to image conversion attempts.


I had no interest in linking my Apple or Google accounts to Adobe, as their site security has been breached in the past, as hundreds of spam emails have confirmed over the years.

Nov 9, 2023 9:17 AM in response to VikingOSX

A clarification is in order. The jpegs do appear as 72dpi, but the relevant aspect is the number of pixels. If I selected 300dpi I get a jpeg with 2481 × 3508 pixels, whereas the same pdf exported with 72dpi selected yields a jpeg with 596 × 842 pixels. So about four times as many pixels, which agrees with 300 vs 72. For display purposes, dpi is irrelevant. For printing purposes, it is not.

However, it should, as you say, respect the dpi setting you chose. It only kind of does.

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.