Friday, October 22, 2021

#1 2021-10-08 01:27:05 pm

Vijay_Yukthi
Member
Registered: 2021-04-16
Posts: 34

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?

Offline

 

#2 2021-10-08 02:53:45 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1089

Re: Convert PDF file to PNG using applescript or shell script

Vijay_Yukthi wrote:

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.

Applescript:

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

Last edited by peavine (2021-10-08 05:03:23 pm)


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

#3 2021-10-09 03:59:32 am

Vijay_Yukthi
Member
Registered: 2021-04-16
Posts: 34

Re: Convert PDF file to PNG using applescript or shell script

Thanks Peavine. Its working fine!

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

Offline

 

#4 2021-10-09 07:23:13 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1089

Re: Convert PDF file to PNG using applescript or shell script

Vijay_Yukthi wrote:

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.


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

#5 2021-10-09 11:38:46 am

Vijay_Yukthi
Member
Registered: 2021-04-16
Posts: 34

Re: Convert PDF file to PNG using applescript or shell script

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

Applescript:

   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

Last edited by Vijay_Yukthi (2021-10-09 11:42:10 am)

Offline

 

#6 2021-10-09 03:29:19 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1089

Re: Convert PDF file to PNG using applescript or shell script

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.

Applescript:

-- 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


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

#7 2021-10-10 01:26:00 am

Vijay_Yukthi
Member
Registered: 2021-04-16
Posts: 34

Re: Convert PDF file to PNG using applescript or shell script

Thanks Peavine! Its Works

Offline

 

#8 2021-10-10 01:36:27 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 901

Re: Convert PDF file to PNG using applescript or shell script

Vijay_Yukthi wrote:


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


PNG supports non-premultiplied alpha only

PNG uses "unassociated" or "non-premultiplied" alpha so that images with separate transparency masks can be stored losslessly. Another common technique, "premultiplied alpha", stores pixel values premultiplied by the alpha fraction; in effect, the image is already composited against a black background. Any image data hidden by the transparency mask is irretrievably lost by that method, since multiplying by a zero alpha value always produces zero.

Some image rendering techniques generate images with premultiplied alpha (the alpha value actually represents how much of the pixel is covered by the image). This representation can be converted to PNG by dividing the sample values by alpha, except where alpha is zero. The result will look good if displayed by a viewer that handles alpha properly, but will not look very good if the viewer ignores the alpha channel.

Although each form of alpha storage has its advantages, we did not want to require all PNG viewers to handle both forms. We standardized on non-premultiplied alpha as being the lossless and more general case.

Info how Photoshop handle PNG
https://helpx.adobe.com/se/experience-m … nnels.html

So what could you do with sips command:
You could try to convert it to bmp (rgb) first and later to PNG

You could also try this to remove the alpha channel
https://github.com/bpolat/Alpha-Channel-Remover

Last edited by Fredrik71 (2021-10-10 01:36:41 am)


if you are the expert, who will you call if its not your imagination.

Offline

 

#9 2021-10-10 08:03:11 am

Vijay_Yukthi
Member
Registered: 2021-04-16
Posts: 34

Re: Convert PDF file to PNG using applescript or shell script

Thanks Fredrick for detailed explanation smile

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)