Skip to content

Commit

Permalink
Merge pull request #84 from danielklim/danielklim/pdfcommand-arg-pass…
Browse files Browse the repository at this point in the history
…through

Pass command line args to wkhtmltopdf to enable better formatting
  • Loading branch information
craig-davis authored Oct 22, 2018
2 parents c5c4f40 + 50e7831 commit d4cfc4c
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions src/Resume/Command/PdfCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@ protected function configure()
InputOption::VALUE_REQUIRED,
'Which of the templates to use'
)
->addOption(
'htmlonly',
'H',
InputOption::VALUE_NONE,
'Only render interim HTML (don\'t run wkhtmltopdf)'
)
->addOption(
'keephtml',
'k',
InputOption::VALUE_NONE,
'Keep interim HTML'
)
->addOption(
'pdfargs',
'p',
InputOption::VALUE_REQUIRED,
'Passthrough arguments for wkhtmltopdf',
'--dpi 300 -s Letter'
)
->addOption(
'output',
'o',
Expand All @@ -48,6 +67,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
$template = $input->getOption('template');
$pdfSource = join(DIRECTORY_SEPARATOR, array($destination, '.tmp_pdf_source.html'));
$optFilename = $input->getOption('output');
$htmlonly = $input->getOption('htmlonly');
$keephtml = $input->getOption('keephtml');
$pdfargs = $input->getOption('pdfargs');

$destFilename = join(DIRECTORY_SEPARATOR, array($destination, pathinfo($source, PATHINFO_FILENAME) . '.pdf'));

Expand Down Expand Up @@ -82,11 +104,24 @@ protected function execute(InputInterface $input, OutputInterface $output)
// Save to a temp destination for the pdf renderer to use
file_put_contents($pdfSource, $rendered);

// command that will be invoked to convert html to pdf
$cmd = "wkhtmltopdf $pdfargs $pdfSource $destFilename";

// Process the document with wkhtmltopdf
exec('wkhtmltopdf --dpi 300 ' . $pdfSource .' ' . $destFilename);
if(!$htmlonly)
exec($cmd);

// Unlink the temporary file
unlink($pdfSource);
if(!($htmlonly || $keephtml))
unlink($pdfSource);
else
$output->writeln(
sprintf(
"Keeping interim HTML: <info>%s</info>",
$pdfSource
),
$this->app->outputFormat
);

$output->writeln(
sprintf(
Expand Down

0 comments on commit d4cfc4c

Please sign in to comment.