Convert PDF file to PNG using applescript or shell script

Hi all,

Is it possible to convert the pdf files in the folder into PNG files using apple script or shell script?

Vijay_Yukthi. The following should do what you want but with one caveat. If a PDF contains more than one page, only the first page is converted to a PNG file. If you want every page of a multipage PDF converted to a separate PNG file, things become a lot more complicated and an entirely different approach is required.

The PNG file created by this script has the same name as the PDF file except with a png extension. If the destination PNG file already exists, it is overwritten without prompting the user.

set sourceFolder to (choose folder)

tell application "Finder" to set sourceFiles to (every file in sourceFolder whose name extension is "pdf") as alias list

repeat with aFile in sourceFiles
	set sourcefile to quoted form of POSIX path of aFile
	set targetFile to (text 1 thru -4 of (aFile as text) & "png")
	set targetFile to quoted form of POSIX path of targetFile
	do shell script "sips --setProperty format png " & sourcefile & " --out " & targetFile
end repeat

Thanks Peavine. Its working fine!

In output PNG everything is fine except the white background is transparent is anyway to sort it out?

The sips utility has a great many options, some of which appear to affect the color and display of an image. Unfortunately, I have no knowledge in this particular area and can’t offer any advice. You may want to look at the sips man page or perhaps another forum member will be able to help.

Thanks Peavine! I have tried different method through acrobat but I got error to convert as png in acrobat writable error(Acrobat could not save a page in this document because of the following error: Error attempting to write to file. This may be due to insufficient disk space (Page 1))

outputLocation is folder path which contains pdf files

	tell application "Adobe Acrobat"
		activate
		tell application "Finder" to set sourceFiles to (every file in outputLocation whose name extension is "pdf") as alias list
		repeat with aFile in sourceFiles
			set sourcefile to quoted form of POSIX path of aFile
			open aFile
			set targetFile to (text 1 thru -4 of (aFile as text) & "png")
			set targetFile to quoted form of POSIX path of targetFile
			save front document to file targetFile using conversion "com.adobe.acrobat.png"
			close front document
		end repeat
	end tell

Vijay_Yukthi. I do not have Adobe Acrobat and hopefully a forum member who does will be able to help. In the meantime, a couple of issues in your script need attention. First, one application tell statement is normally not included in another application tell statement. Second, applications like Adobe Acrobat seldom work with files with a POSIX path and instead use either aliases or files with HFS paths. So, the following is just a guess but demonstrates a possible solution.

-- outputLocation is set elsewhere in the script and is an alias
tell application "Finder" to set sourceFiles to (every file in outputLocation whose name extension is "pdf") as alias list

tell application "Adobe Acrobat"
     activate
     repeat with aFile in sourceFiles
         open aFile
         set targetFile to (text 1 thru -4 of (aFile as text) & "png")
         save front document to file targetFile using conversion "com.adobe.acrobat.png"
         close front document
     end repeat
 end tell

Thanks Peavine! Its Works

Thanks Fredrick for detailed explanation :slight_smile: