Skip to content

Instantly share code, notes, and snippets.

@gmalysa
Created November 21, 2018 21:22
Show Gist options
  • Save gmalysa/291cc111e92f275e51c2f690249fb24e to your computer and use it in GitHub Desktop.
Save gmalysa/291cc111e92f275e51c2f690249fb24e to your computer and use it in GitHub Desktop.
<?php
/**
* Convert a series of TIFF files given on the command line to pages in
* a single PDF
*/
if ($argc < 3) {
echo ('Convert a series of TIFF images as individual pages in a PDF');
echo ("\n".'Usage: php -f pdf_convert.php <output name> <input 1> <input 2> ...');
exit(0);
}
// Get file names from command line
$output_file = $argv[1];
$input_files = array();
for ($i = 2; $i < $argc; ++$i) {
$input_files[] = $argv[$i];
}
// Initialize PDF File
$pdf = new PDFLib();
if ($pdf->begin_document('', '') == 0) {
die('Error: '.PDF_get_errmsg($pdf));
}
// For each of the input images, load them, create an 8.5" x 11" page, place them as
// the full page, and then close the page
foreach ($input_files as $in_file) {
echo ('Now processing '.$in_file.'...'."\n");
$pdf->begin_page_ext(612, 792, '');
$image = $pdf->load_image('auto', $in_file, '');
$pdf->fit_image($image, 0, 0, 'boxsize {612 792} fitmethod meet');
$pdf->close_image($image);
$pdf->end_page_ext('');
}
$pdf->end_document('');
$raw_pdf = $pdf->get_buffer();
file_put_contents($output_file, $raw_pdf);
echo ('Done!'."\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment