darkmist icon

convert2pdf.php

darkmist | PRO | 01/22/16 04:31:24 PM UTC | 0 ⭐ | 209 👁️ | Never ⏰ | []
PHP |

2.97 KB

|

None

|

0 👍

/

0 👎

<?php
 
// Unzip the file
$filename = $argv[1];
$directory = pathinfo($filename, PATHINFO_FILENAME);
 
echo 'Unziping: ' . $filename . PHP_EOL;
 
$zip = new ZipArchive;
 
if ($zip->open($filename) === false) {
    die('Failed to open ' . $filename);
}
 
mkdir($directory);
$zip->extractTo($directory);
$zip->close();
 
// Read the .srt files from the directory
foreach (new DirectoryIterator($directory) as $fileInfo) {
    if ($fileInfo->isDot()) {
        continue;
    }
 
    $filename = $fileInfo->getFilename();
    echo 'Parsing: ' . $filename . PHP_EOL;
    $files[pathinfo($filename, PATHINFO_FILENAME)] =
        file_get_contents($directory.'/'.$filename);
}
foreach ($files as $key => $value) {
    $values = explode(PHP_EOL, $value);
    $newValue = '';
    foreach ($values as $line) {
        $line = trim($line);
        if (empty($line)) {
            continue;
        }
        if ((strpos($line, ':') !== false) && (strpos($line, ',') !== false)) {
            continue;
        }
 
        $newValue .= $line . PHP_EOL;
    }
    $files[$key] = $newValue;
}
 
// Create and populate the new PDF file
try {
    echo 'Creating: ' . $directory . '.pdf' . PHP_EOL;
    $p = new PDFlib();
    if ($p->begin_document($directory . '.pdf', '') == 0) {
        die('ERROR: ' . $p->get_errmsg());
    }
    $p->set_info("Creator", __FILE__);
    $p->set_info("Author", "Georgia Tech");
    $p->set_info("Title", $directory);
 
    $texty = 802;
    $p->begin_page_ext(595, 842, '');
 
    foreach ($files as $key => $value) {
        $font = $p->load_font('Times-BoldItalic', 'iso8859-1', '');
        $p->setfont($font, 18.0);
        $p->show_xy(' ', 50, $texty);
        $p->continue_text($key);
        $p->continue_text(' ');
        $lines = explode('|', wordwrap($value, 100, '|'));
        foreach ($lines as $line) {
            $font = $p->load_font('Times-Roman', 'iso8859-1', '');
            $p->setfont($font, 12.0);
            $p->continue_text($line);
            $p->continue_text(' ');
 
            $texty = $p->get_value('texty', 0);
            if ($texty <= 60) {
                $p->end_page_ext('');
                $texty = 802;
                $p->begin_page_ext(595, 842, '');
                $font = $p->load_font('Times-Roman', 'iso8859-1', '');
                $p->setfont($font, 12.0);
                $p->show_xy(' ', 50, $texty);
            }
        }
        $texty = $p->get_value('texty', 0);
        if ($texty <= 60) {
            $p->end_page_ext('');
            $texty = 802;
            $p->begin_page_ext(595, 842, '');
        } else {
            $p->continue_text(' ');
        }
    }
    $p->end_page_ext('');
    $p->end_document("");
} catch (PDFlibException $e) {
    die("PDFlib exception occurred:\n" .
    "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
    $e->get_errmsg() . "\n");
} catch (Exception $e) {
    die($e);
}
$p = 0;
exec('rm -rf "' . $directory . '"');

Comments