diff --git a/.gitignore b/.gitignore index a136ee1..0c32af8 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ vendor resume/_*.md output/*.html output/*.pdf +.tmp_pdf_source.html # OSX files .DS_Store diff --git a/README.md b/README.md index 6dde09f..fa82ff3 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ at the [blog post for the project][blog]. ## Features * Three styles to choose from: modern, blockish, unstyled -* PDF generation via `wkhtmltopdf` +* PDF generation via [wkhtmltopdf][wkhtmltopdf] * Responsive design for multiple device viewport sizes * Simple Markdown formatting * Single file deployment @@ -109,3 +109,4 @@ have been added. It's a nice template, and if you are a more comfortable with ht [srt]: http://sampleresumetemplate.net/ "A great starting point" [blog]: http://there4development.com/blog/2012/12/31/markdown-resume-builder/ [pake]: https://github.com/indeyets/pake/wiki/Installing-Pake +[wkhtmltopdf]: https://github.com/pdfkit/pdfkit/wiki/Installing-WKHTMLTOPDF diff --git a/src/Resume/Command/HtmlCommand.php b/src/Resume/Command/HtmlCommand.php index 5ad5f34..ef1c138 100644 --- a/src/Resume/Command/HtmlCommand.php +++ b/src/Resume/Command/HtmlCommand.php @@ -54,6 +54,20 @@ class HtmlCommand extends Command $template = $input->getOption('template'); $refresh = $input->getOption('refresh'); + $rendered = $this->generateHtml($source, $template, $refesh); + file_put_contents($destination, $rendered); + $output->writeln( + sprintf( + "Wrote resume to: %s", + $destination + ), + $this->app->outputFormat + ); + return true; + } + + protected function generateHtml($source, $template, $refresh) { + // Check that the source file is sane if (!file_exists($source)) { $output->writeln( @@ -124,16 +138,7 @@ class HtmlCommand extends Command ) ); - // Save the fully rendered html to the final destination - file_put_contents($destination, $rendered); - $output->writeln( - sprintf( - "Wrote resume to: %s", - $destination - ), - $this->app->outputFormat - ); - + return $rendered; } } diff --git a/src/Resume/Command/PdfCommand.php b/src/Resume/Command/PdfCommand.php index 5a89179..c6ee833 100644 --- a/src/Resume/Command/PdfCommand.php +++ b/src/Resume/Command/PdfCommand.php @@ -1,13 +1,14 @@ addArgument( - 'output', + 'destination', InputArgument::REQUIRED, - 'Output html document' + 'Output pdf document' ) ->addOption( 'template', @@ -34,6 +35,53 @@ class PdfCommand extends Command protected function execute(InputInterface $input, OutputInterface $output) { + $this->app = $this->getApplication(); + $source = $input->getArgument('source'); + $destination = $input->getArgument('destination'); + $template = $input->getOption('template'); + $pdfSource = join(DIRECTORY_SEPARATOR, array(dirname($destination), '.tmp_pdf_source.html')); + + // Make sure we've got out converter available + exec('wkhtmltopdf -V', $results, $returnVal); + if ($returnVal) { + $output->writeln( + sprintf( + "\nError: Unable to locate wkhtmltopdf.\n" . + " Please make sure that it is installed and available in " . + "your path. \n For installation help, please read: " . + "https://github.com/pdfkit/pdfkit/wiki/Installing-WKHTMLTOPDF \n\n", + $destination + ), + $this->app->outputFormat + ); + return false; + } + + $rendered = $this->generateHtml($source, $template, false); + + // The pdf needs some extra css rules, and so we'll add them here + // to our html document + // TODO: Update this with the simple DOM to add class + $rendered = str_replace('body class=""', 'body class="pdf"', $rendered); + + // Save to a temp destination for the pdf renderer to use + file_put_contents($pdfSource, $rendered); + + // Process the document with wkhtmltopdf + exec('wkhtmltopdf ' . $pdfSource .' ' . $destination); + + // Unlink the temporary file + unlink($pdfSource); + + $output->writeln( + sprintf( + "Wrote pdf resume to: %s", + $destination + ), + $this->app->outputFormat + ); + + return true; } }