Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion bin/pdf2images.cmd
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
find.exe ./tests/_files -type f -name "*.pdf" | xargs -n1 sh -c 'magick -verbose -density 300 "$0[0]" -background white -flatten "${0%%.pdf}.png"'
@echo off
setlocal

pushd "%~dp0.."
for /r "tests\_files" %%F in (*.pdf) do (
magick -verbose -density 300 "%%~fF[0]" -background white -flatten "%%~dpnF.png"
)
popd
67 changes: 67 additions & 0 deletions bin/pdf2images.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
param(
[string]$Root = "tests/_files",
[int]$Density = 300,
[switch]$VerboseMagick,
[switch]$FirstPageOnly = $true
)

$ErrorActionPreference = 'Stop'

$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$repoRoot = Resolve-Path (Join-Path $scriptDir "..")
Set-Location $repoRoot

Write-Host "Working directory: $repoRoot" -ForegroundColor Cyan

if (-not (Get-Command magick -ErrorAction SilentlyContinue)) {
throw "ImageMagick 'magick' was not found in PATH."
}

if (-not (Test-Path $Root)) {
throw "Root path '$Root' does not exist."
}

$files = @(Get-ChildItem -Path $Root -Filter *.pdf -Recurse -File)
$total = $files.Count

if ($total -eq 0) {
Write-Host "No PDF files found in '$Root'." -ForegroundColor Yellow
exit 0
}

Write-Host "Found $total PDF file(s) in '$Root'. Converting..." -ForegroundColor Cyan

$index = 0
foreach ($file in $files) {
$index++
$pdf = $file.FullName
$png = [System.IO.Path]::ChangeExtension($pdf, 'png')

Write-Host "[$index/$total] $($file.Name)" -NoNewline

$source = if ($FirstPageOnly) { "$pdf`[0`]" } else { $pdf }
$magickArgs = @()

if ($VerboseMagick) {
$magickArgs += '-verbose'
}

$magickArgs += @(
'-density', $Density,
$source,
'-background', 'white',
'-flatten',
$png
)

& magick @magickArgs
if ($LASTEXITCODE -ne 0) {
Write-Host " [FAILED]" -ForegroundColor Red
throw "ImageMagick failed for '$pdf'"
}

Write-Host " -> $([System.IO.Path]::GetFileName($png))" -ForegroundColor Green
}

Write-Host ""
Write-Host "Done. $total file(s) converted." -ForegroundColor Cyan
Loading
Loading