How can I convert a .pdf file to .png programmatically?

Hello,

I just created a .pdf file programmatically, I now want to convert that .pdf file to a .png file.

How can I do that?

Thanks.

Lennox

more details would help
what platform ? (macOS, Windows, Linux ?)
what kind of app (desktop, web, console)

and check whatever plugins you have as there may be a way built in to them

OK Norman,

It’s macOS Sierra

It’s a desktop app.

No plugins in use and prefer not to achieve this with plugins.

Thanks.

Lennox

sips -s format png your_pdf.pdf --out your_png.png

or a script for multiple

for i in [filename]; do sips -s format [image type] $i --out [destination]/$i.[extension];done

Thanks Axel,

sips -s format png your_pdf.pdf --out your_png.png is concise but is above my coding skills.

Dim pdfFile, pngFile As Folderitem
pdfFile = myFolder.Child(“myFile.pdf”)
pngFile = myFolder.Child(“myFile.png”)

How can I achieve that?

Thanks.

Lennox

  Dim pdfFile, pngFile As Folderitem
  pdfFile = myFolder.Child("myFile.pdf")
  pngFile = myFolder.Child("myFile.png")
  
  dim sh as new Shell
  dim cmd as String
  
  if pdfFile <> nil and pdfFile.Exists then
    if pngFile <> nil then
      cmd = "sips -s format png " + pdfFile.ShellPath + " --out " + pngFile.ShellPath
      sh.Execute (cmd)
      
      if sh.ErrorCode <> 0 then
        MsgBox sh.ReadAll
      end if
    end if
  end if

you can convert all pdf in a folder (untested)

you get a new Folder ‘Converted’ with all converted files.

dim sh as new Shell
dim cmd as String
dim f as FolderItem = GetFolderItem("/myDisk/myFolder") // path to your folder

if f <> nil and f.Exists then
cmd = "cd " + f.ShellPath + “;for i in *.pdf do sips -s format png $i --out Converted/$i.png;done”
sh.Execute (cmd)
if sh.ErrorCode <> 0 then
MsgBox sh.ReadAll
end if
end if

Thanks Axel.

Works great.

Lennox

Hi Axel,

The .pdf file has a white background and the .png file has a grey background.

Is it possible to modify the code so as to get the original white background?

Thanks again.

Lennox

Perhaps the PDF has a transparent background?

OK Axel,

Thanks.

Lennox