1

我正在尝试从 PHP 页面(Apache、LAMP)的 HTML 文件创建 PDF 奇怪的是,当我从命令行执行脚本时,它可以正常工作并按预期创建 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 命令。

帮助!?

4

2 回答 2

0

我已经看到了同样的问题,在我的一生中,我根本找不到为什么它不能通过基于网络的调用来解决问题的答案,但从命令行从来没有问题。因此,我没有在这方面苦苦寻找解决方案,而是创建了一个 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;
于 2019-06-28T05:37:12.920 回答
0

您可能会尝试使用完整路径,因为您的 php 文件可能在与保存的目录不同的目录中执行,具体取决于它的加载方式。include(通过,require.htaccess直接通过 apache进行 IE 。)

IE

$htmlName = ("/home/alex/html/output2/alex" . time() . ".html");
$pdfName = ("/home/alex/html/output2/output2/alex" . time() . ".pdf");

我同意使用http://dompdf.github.io/https://tcpdf.org/之类的包最好不过的评论。

于 2017-01-15T00:37:11.433 回答