我正在尝试从PHP页面的HTML文件创建PDF (Apache,LAMP) Wierd的事情是,当我从命令行执行脚本,它的工作和创建PDF的预期。但是,当我在浏览器中浏览该页面时,它什么也不做。我认为这是某处的权限问题,但我被难住了!这是代码。(注意ls命令确实会在浏览器中产生输出,所以这不仅仅是PHP不允许执行shell命令的问题)
<?php
$htmlName = ("output2/alex" . time() . ".html");
$pdfName = ("output2/alex" . time() . ".pdf");
$html = "<html><body><h1>Hello, World!</h1></body></html>";
$fileHandle = fopen($htmlName, "w+");
fwrite($fileHandle, $html);
fclose($fileHandle);
$command= "htmldoc -t pdf --browserwidth 1000 --embedfonts --header ... --footer t./ --headfootsize 5.0 --fontsize 9 --bodyfont Arial --size letter --top 4 --bottom 25 --left 28 --right 30 --jpeg --webpage $options '$htmlName' -f '$pdfName'";
echo "OUTPUT: \r\n";
$X=passthru($command);
echo "TESTING LS:";
$y=passthru("ls -al");
if(file_exists($htmlName) && file_exists($pdfName)) {
echo "Success.";
} else {
echo "Sorry, it did not create a PDF";
}
?>当我从命令行执行脚本时,它会产生预期的输出,并按照预期创建一个PDF文件:
> php alextest.php
Zend OPcache requires Zend Engine API version 220131226.
The Zend Engine API version 220100525 which is installed, is outdated.
OUTPUT:
PAGES: 1
BYTES: 75403
TESTING LS:total 2036
drwxr-xr-x 9 ----- and so on...当我在Chrome中浏览页面时,它只输出LS命令。
救命!?
发布于 2017-01-15 08:37:11
您可以尝试使用完整路径,因为您的php文件可能会在不同的目录中执行,而不是保存在其他目录中,这取决于它是如何加载的。(即通过include、require或.htaccess或直接通过apache。)
即
$htmlName = ("/home/alex/html/output2/alex" . time() . ".html");
$pdfName = ("/home/alex/html/output2/output2/alex" . time() . ".pdf");我同意这样的评论,使用像http://dompdf.github.io/或https://tcpdf.org/这样的包是最好的。
发布于 2019-06-28 13:37:13
我也看到过同样的问题,我就是找不到答案,为什么它不能从基于web的调用中做到这一点,但从命令行中从来没有问题。因此,我创建了一个Perl代理,允许我从命令行解析PDF,从而使它几乎可以用于任何给定的目的,而不是在这方面努力寻找解决方案。因为,使用Perl,我在解析PDF时从来没有遇到过问题,而且我已经做了几十年了。
所以,这就是你要做的。PHP代码:
exec("/usr/local/bin/perl5 -s /path/to/perl/executable/parse-pdf-from-html-document.pl -source=markup-file.html",$output);
foreach ($output as $aline) {
#- WAS SUCCESSFUL
if (strstr($aline,'Successful!') == TRUE) {
#- no feedback, win silently
}
#- NOT SUCCESSFUL
else {
echo $aline . "\n";
}
}$output保存运行exec的结果。
现在让我们看一下parse-pdf-from-html-document.pl的Perl代码:
#!/usr/local/bin/perl5 -s
#- $document coming from commandline variable, via caller: PHP script
$myDocumentLocale = "/path/to/markup/document/".$document;
if (-e $myDocumentLocale) {
$documentHTML = $myDocumentLocale;
$documentPDF = $documentHTML;
$documentPDF =~ s/\.html/\.pdf/gi;
$myDocumentHTML = `cat $myDocumentLocale`;
$badPDF = 0;
$myPDFDocumentLocale = $myDocumentLocale;
$myPDFDocumentLocale =~ s/\.html/\.pdf/gi;
$badPDF = &parsePDF($myDocumentLocale, $myPDFDocumentLocale);
if ($badPDF == 0) {
print "Successful!";
}
else {
print "Error: No PDF Created.";
}
exit;
}
else {
print "Error: No document found.";
exit;
}
sub parsePDF {
my ($Ihtml, $Ipdf) = @_;
$wasBad = 0;
#- create PDF
$ENV{HTMLDOC_NOCGI} = 1;
$commandline="/usr/local/bin/htmldoc -t pdf13 --pagemode document --header ... --footer ... --left 1cm --size Letter --webpage -f $Ipdf $Ihtml";
select(STDOUT);
$| = 1;
#print "Content-Type: application/pdf\n\n";
system($commandline);
if (-e $Ipdf) {
$wasBad = 0;
}
else {
$wasBad = 1;
}
return $wasBad;
}
exit;https://stackoverflow.com/questions/41655356
复制相似问题