Skip to content

Instantly share code, notes, and snippets.

@emonkak
Last active October 28, 2019 10:33
Show Gist options
  • Save emonkak/fc960e214dd132e0a0e7aafdba4eb761 to your computer and use it in GitHub Desktop.
Save emonkak/fc960e214dd132e0a0e7aafdba4eb761 to your computer and use it in GitHub Desktop.
pdf2jpg
@echo off
powershell -NoProfile -ExecutionPolicy Unrestricted .\pdf2jpg.ps1
echo Done.
pause > nul
#!/bin/bash
set -euo pipefail
if ! which pdftoppm &>/dev/null
then
echo Unable to find pdftoppm 1>&2
exit 1
fi
if [ $# -ge 1 ]
then
target_file="$1"
else
target_file=`osascript -e 'tell app (path to frontmost application as Unicode text) to set new_file to POSIX path of (choose file with prompt "Select A PDF" of type {"PDF"})' 2> /dev/null`
fi
if [ ! -f "$target_file" ]
then
echo File "'$target_file'" does not exist 1>&2
exit 1
fi
target_directory="${target_file%/*}"
target_basename="${target_file##*/}"
target_basename="${target_basename%.*}"
output_directory="$target_directory/$target_basename"
output_path="$output_directory/$target_basename"
if [ ! -d "$output_directory" ]
then
mkdir -p "$output_directory"
fi
exec pdftoppm "$target_file" -jpeg -jpegopt quality=90 -scale-to-x 1280 -scale-to-y -1 "$output_path"
param([string] $fileName)
$commandCandidates = "pdftoppm.exe", ".\pdftoppm.exe", ".\bin\pdftoppm.exe", "C:\msys32\mingw32\bin\pdftoppm.exe", "C:\msys64\mingw64\bin\pdftoppm.exe"
$command = $null
foreach ($commandCandidate in $commandCandidates)
{
if ((Get-Command $commandCandidate -ErrorAction SilentlyContinue) -ne $null)
{
$command = $commandCandidate
break
}
}
if ($command -eq $null)
{
Write-Host "Unable to find pdftoppm.exe."
exit
}
if ($fileName -eq "")
{
if ([Threading.Thread]::CurrentThread.GetApartmentState() -eq "MTA")
{
PowerShell.exe -sta -file $MyInvocation.MyCommand.Path
exit
}
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.forms")
$dialog = New-Object System.Windows.Forms.OpenFileDialog
$dialog.Filter = "PDF (*.pdf)|*.pdf"
if ($dialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK)
{
$fileName = $dialog.FileName
}
}
if ($fileName -eq "")
{
Write-Host "File not specified."
exit
}
if (!(Test-Path $fileName -PathType Leaf))
{
Write-Host "File '$($fileName)' does not exist."
exit
}
$file = Get-Item $fileName
$outputDirectory = Join-Path $file.DirectoryName $file.BaseName
if (!(Test-Path $outputDirectory -PathType Container))
{
New-Item -ItemType Directory -Path $outputDirectory
}
$outputPath = Join-Path $outputDirectory $file.BaseName
& $command $file.FullName -jpeg -jpegopt quality=90 -scale-to-x 1280 -scale-to-y -1 $outputPath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment