Recently I had a lot of PDF files that I needed to convert to images. I needed it mostly for compression purposes. To get this done I started with experiments with GhostScript. However I didn’t get the results I wanted mostly because of my experience with it. So after a while I switched to ImageMagick and it ended up being pretty straight forward as well as delivering very nice compression results.
So after doing 1 file I created and programmed the script around to handle input from the pipeline by the FileInfo object so that you could use Get-ChildItem as input recursively if you wanted that.
The end result? I did Get-ChildItem C:\path -Filter “*.pdf” | Export-Image and awaaaay we go. The script defaults to a density of 300 and with the extension being PNG but you can change it if you like.
The script is not perfect or anything but it works.
<#
.SYNOPSIS
This is a simple Powershell script that allows you to export images from a PDF file based on various preferences.
.DESCRIPTION
The script uses ImageMagick to export images from a PDF based on various command line switch options based on your preferences.
It uses FileInfo input so Get-ChildItem or a direct file can work.
.EXAMPLE
./Export-Image -File C:\example.pdf -Density 300 -Quality 100 -Extension "png"
.LINK
https://perplexity.nl
https://imagemagick.org/
https://imagemagick.org/script/download.php
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)][System.IO.FileInfo]$File,
[ValidateSet('png', 'gif', 'jpg', 'jpeg')][string]$Extension = "png",
[ValidateRange(1, 1200)][int]$Density = 300,
[ValidateRange(1, 100)][int]$Quality = 100,
[switch]$Overwrite,
[string]$ImageMagick
)
Begin
{
if (-Not ($ImageMagick))
{
$ImageMagick = "C:\Program Files\ImageMagick-7.0.9-Q16\magick.exe"
}
if (-Not(Test-Path $ImageMagick))
{
Write-Error "The ImageMagick installation path file you selected does not exist, please (re)install and try again"
Exit
}
}
Process
{
$Destination = $File.FullName -replace ($File.Extension), ("." + $Extension)
if (-Not ($Overwrite))
{
if (Test-Path $Destination)
{
Write-Error "$Destination already exists, please use the Overwrite switch to force overwriting the destination file"
Exit
}
}
else
{
Remove-Item $Destination -Force -ErrorAction SilentlyContinue
}
$Arguments = '/c magick -density ' + $Density + ' -quality ' + $Quality + ' "' + $File.FullName + '" "' + $Destination + '"'
Write-Verbose $Arguments
Start-Process cmd -ArgumentList $Arguments -Wait -WindowStyle hidden
}
Thanks for making this.
I wanted to make a quick mention to you though that this script kept failing on me until I figured out what was happening after about a half hour. As it turns out, the word “Extention” in the script is misspelled and should be instead “Extension”. Using the script without correcting this word causes line 45 to fail the as demonstrated below:
PS C:\Temp> .\Export-Image.ps1 -File C:\Temp\test.pdf -Extention jpeg -Verbose
VERBOSE: /c magick -density 300 -quality 100 “C:\Temp\test.pdf” “.jpegC.jpeg:.jpeg\.jpegT.jpege.jpegm.jpegp.jpeg\.jpegt.jpege.jpegs.jpegt.jpeg..jpegp.jpegd.jpegf.jpeg”
As you can see, it appends each letter of the file path to “.jpeg” until it is finished.
However, as previously mentioned, changing all instances of “Extention” to “Extension” fixed this up.
Hello mahatah,
Thank you for the reply that means a lot to me. I will make a change to fix it as well. It is a translation thing. My apologies!
Jeff