-
Notifications
You must be signed in to change notification settings - Fork 517
/
PdfCommand.php
138 lines (123 loc) · 4.62 KB
/
PdfCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
namespace Resume\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Sunra\PhpSimple\HtmlDomParser;
class PdfCommand extends HtmlCommand
{
protected function configure()
{
$this
->setName('pdf')
->setDescription('Generate a PDF from a markdown file')
->addArgument(
'source',
InputArgument::REQUIRED,
'Source markdown document'
)
->addArgument(
'destination',
InputArgument::REQUIRED,
'Output destination folder'
)
->addOption(
'template',
't',
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',
InputOption::VALUE_REQUIRED,
'The optional override of default filename to output to'
);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->app = $this->getApplication();
$source = $input->getArgument('source');
$sourceName = pathinfo($source, PATHINFO_FILENAME);
$destination = rtrim($input->getArgument('destination'), DIRECTORY_SEPARATOR);
$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'));
if ($optFilename) {
$destFilename = $destination . DIRECTORY_SEPARATOR . $optFilename . '.pdf';
} else {
$destFilename = $destination . DIRECTORY_SEPARATOR . $sourceName . '.pdf';
}
// Make sure we've got out converter available
exec('wkhtmltopdf -V', $results, $returnVal);
if ($returnVal) {
$output->writeln(
"\n<error>Error:</error> 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",
$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
$simpleDom = HtmlDomParser::str_get_html($rendered);
$body = $simpleDom->find('body', 0);
$body->class = $body->class . ' pdf';
$rendered = (string) $simpleDom;
// 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
if(!$htmlonly)
exec($cmd);
// Unlink the temporary file
if(!($htmlonly || $keephtml))
unlink($pdfSource);
else
$output->writeln(
sprintf(
"Keeping interim HTML: <info>%s</info>",
$pdfSource
),
$this->app->outputFormat
);
$output->writeln(
sprintf(
"Wrote pdf resume to: <info>%s</info>",
$destFilename
),
$this->app->outputFormat
);
return true;
}
}
/* End of file PdfCommand.php */