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
File renamed without changes.
58 changes: 17 additions & 41 deletions bin/pdf2images.ps1
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
param(
[string]$Root = "tests/_files",
[int]$Density = 300,
[switch]$VerboseMagick,
[switch]$FirstPageOnly = $true
[int]$Density = 300
)

$ErrorActionPreference = 'Stop'
$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
Set-Location (Resolve-Path (Join-Path $PSScriptRoot ".."))

if (-not (Get-Command magick -ErrorAction SilentlyContinue)) {
throw "ImageMagick 'magick' was not found in PATH."
Expand All @@ -21,47 +15,29 @@ if (-not (Test-Path $Root)) {
throw "Root path '$Root' does not exist."
}

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

if ($total -eq 0) {
if ($pdfs.Count -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 "Found $($pdfs.Count) PDF file(s). Converting..." -ForegroundColor Cyan

Write-Host "[$index/$total] $($file.Name)" -NoNewline
foreach ($pdf in $pdfs) {
$png = [IO.Path]::ChangeExtension($pdf.FullName, "png")

$source = if ($FirstPageOnly) { "$pdf`[0`]" } else { $pdf }
$magickArgs = @()
Write-Host "$($pdf.Name)" -NoNewline

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

$magickArgs += @(
'-density', $Density,
$source,
'-background', 'white',
'-flatten',
magick `
-density $Density `
"$($pdf.FullName)[0]" `
-background white `
-flatten `
-define png:exclude-chunk=tIME,tEXt,zTXt `
$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 " -> $([IO.Path]::GetFileName($png))" -ForegroundColor Green
}

Write-Host ""
Write-Host "Done. $total file(s) converted." -ForegroundColor Cyan
Write-Host "Done. $($pdfs.Count) file(s) converted." -ForegroundColor Cyan
1 change: 1 addition & 0 deletions dev/autoload.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?php

require_once __DIR__ . '/../autoload.php';

4 changes: 4 additions & 0 deletions dev/dev-multicell-shrinking.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
/**
* Pdf Advanced Multicell - Example
*/
require_once __DIR__ . '/autoload.php';

use EvoSys21\PdfLib\Dev\DevFactory;

$factory = new DevFactory();

// Create the Advanced Multicell Object and inject the PDF object
Expand Down
4 changes: 4 additions & 0 deletions dev/dev-multicell-shrinking3.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
/**
* Pdf Advanced Multicell - Example
*/
require_once __DIR__ . '/autoload.php';

use EvoSys21\PdfLib\Dev\DevFactory;

$factory = new DevFactory();

// Create the Advanced Multicell Object and inject the PDF object
Expand Down
1 change: 1 addition & 0 deletions dev/dev-multicell-style.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Pdf Advanced Multicell - Example
*/

use EvoSys21\PdfLib\Dev\DevFactory;
use EvoSys21\PdfLib\Tools;

$factory = new DevFactory();
Expand Down
1 change: 1 addition & 0 deletions dev/dev-multicell-style1.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Pdf Advanced Multicell - Example
*/

use EvoSys21\PdfLib\Dev\DevFactory;
use EvoSys21\PdfLib\Tools;

$factory = new DevFactory();
Expand Down
Loading
Loading