使用Pdfcrowd在PHP中将HTML转换为PDF

时间:2015-10-12 11:12:36

标签: php html pdf pdf-generation

我编写了以下PHP函数来尝试生成PDF:

function createPDF($htmlContent) {
    $todayDate = date('Y-m-d--H-i-s');
    $this->pdfFilename = $this->upload_dir['basedir'] . '/rns/rns--'. $todayDate . '.pdf';

    echo "<br><br>Creating: ". $this->pdfFilename ."<br>";

    try {
        // create an API client instance
        $client = new Pdfcrowd($username, $apikey);

        $out_file = fopen($this->pdfFilename, "wb");

        echo "<br>HTML:<br><br>" . $htmlContent . "<br><br><br>";

        $client->convertHtml($htmlContent, $out_file);
        fclose($out_file);
    }
    catch(PdfcrowdException $why) {
        echo "Pdfcrowd Error: " . $why . "<br>";
    }

}

在浏览器中运行时,此回声$htmlContent正确显示(我要转换的HTML,从<html>转换为</html>标记,因此此变量肯定包含HTML),然后出现以下错误:

Pdfcrowd错误:[400]无法转换数据。缺少src字段。

显然有些问题是错误的,但是在我传递给convertHTML()函数之前,src字段并没有丢失,因为它完全没问题。我不确定如何继续,因为pdfcrowd似乎没有关于此错误的其他信息或文档。

(Pdfcrowd文档here

如果我复制并粘贴来自$htmlContent的echo'd html并将其放入convertHTML()的第一个参数中,则代码可以正常运行...那么为什么我不能将其作为变量传递?不应该是一样的吗?

有人可以帮忙吗?

注意:我对其他解决方案持开放态度,但我尝试过:html2pdf,fpdf,tcpdf,dompdf&amp; mpdf已经。所有这些都有不同的问题,我需要转换(RNS announcements from London Stock Exchange)的html,它可以包含表格和项目符号。查看我的other question。我将这些RNS网址放入pdfcrowd online converter后得到了很好的结果,所以看起来如果我绕过这个错误就行了......

XML Feed的示例(请参阅注释),这些可以包含多于1个公告:

<Announcement id_pub="12492736" id_rns="4614Y17" cd_ann_type="POS" cd_ann_med="AFX" id_comp="" id_announcement="12492736" cd_tidm="MKS" id_cont="" cd_ann_corr="" fl_regulatory="Y" fl_copyback="" ts_rel="20150908174008" ts_ann_rec="20150908174008" cd_comp_type="" id_prev_rns="" id_par_rns_num="">
<tx_ann_headline>Transaction in Own Shares</tx_ann_headline>
<nm_comp>Marks & Spencer Grp</nm_comp>
<nm_long_comp>Marks & Spencer Group PLC</nm_long_comp>
<nm_cont_long/>
<tx_ann/>
<ht_ann>
<![CDATA[
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=us-ascii" /> <title></title> <meta name="generator" content="RNS" /> <style type="text/css"> /**/ H1 { MARGIN: 24pt 0cm 0pt; FONT-FAMILY: "Cambria","serif"; COLOR: #365f91; FONT
]]>
<![CDATA[
-SIZE: 14pt } H2 { MARGIN: 10pt 0cm 0pt; FONT-FAMILY: "Cambria","serif"; COLOR: #4f81bd; FONT-SIZE: 13pt } H3 { MARGIN: 10pt 0cm 0pt; FONT-FAMILY: "Cambria","serif"; COLOR: #4f81bd; FONT-SIZE: 12pt } H4 { FONT-STYLE: italic; MARGIN: 10pt 0cm 0pt; FONT-FAMILY: "Cambria","serif
]]>
<![CDATA[
"; COLOR: #4f81bd; FONT-SIZE: 12pt } H5 { MARGIN: 10pt 0cm 0pt; FONT-FAMILY: "Cambria","serif"; COLOR: #243f60; FONT-SIZE: 12pt; FONT-WEIGHT: normal } H6 { FONT-STYLE: italic; MARGIN: 10pt 0cm 0pt; FONT-FAMILY: "Cambria","serif"; COLOR: #243f60; FONT-SIZE: 12pt; FONT-WEIGHT: normal }
]]>
<![CDATA[
STRONG { FONT-FAMILY: "Times New Roman","serif" } EM { FONT-FAMILY: "Times New Roman","serif" } OL { MARGIN-BOTTOM: 0cm } UL { MARGIN-BOTTOM: 0cm } LINK { COLOR: blue } visited { COLOR: purple } BODY.ca { TEXT-JUSTIFY-TRIM: punctuation } .bz {
]]>
<![CDATA[
MARGIN: 72pt 90pt; size: 612.0pt 792.0pt } DIV.bz { } P.cn { MARGIN: 0cm 0cm 0pt; FONT-FAMILY: "Times New Roman","serif"; FONT-SIZE: 12pt; FONT-WEIGHT: bold } SPAN.cl { FONT-FAMILY: "Arial","sans-serif"; FONT-SIZE: 11pt; FONT-WEIGHT: bold } P.a { MARGIN: 0cm 0cm 0pt; FONT-FAMILY: "Times New Roman","serif"; FONT-SIZE: 12pt } LI.a { MARGIN: 0cm 0cm 0pt; FONT-FAMILY: "Times New Roman","serif"; FONT-SIZE: 12pt } DIV.a { MARGIN: 0cm 0cm 0pt; FONT-FAMILY: "Times New Roman","serif"; FONT-SIZE: 12pt } SPAN.cj { FONT-FAMILY: "Arial","sans-serif"; FONT-SIZE: 11pt } P.co { TEXT-ALIGN: justify; MARGIN: 0cm 0cm 0pt; FONT-FAMILY: "Times New Roman","serif"; FONT-SIZE: 12pt } SPAN.ci { FONT-FAMILY: "Arial","sans-serif"; FONT-SIZE: 11pt } SPAN.cp { FONT-FAMILY: "Arial","sans-serif"; COLOR: windowtext; FONT-SIZE: 11pt } P.cq { TEXT-ALIGN: justify; MARGIN: 0cm 0cm 0pt; FONT-FAMILY: "Times New Roman","serif"; FONT-SIZE: 12pt; FONT-WEIGHT: bold } SPAN.cf { FONT-FAMILY: "Arial","sans-serif"; FONT-SIZE: 11pt; FONT-WEIGHT: bold } P.cr { TEXT-ALIGN: center; MARGIN: 0cm 0cm 0pt; FONT-FAMILY: "Times New Roman","serif"; FONT-SIZE: 12pt; FONT-WEIGHT: bold } SPAN.cd { FONT-FAMILY: "Arial","sans-serif"; FONT-SIZE: 11pt; FONT-WEIGHT: bold } SPAN.cb { FONT-WEIGHT: bold } /**/ </style> </head> <body class="ca"><div>RNS Number : 4614Y</div> <div>Marks &amp; Spencer Group PLC</div> <div>08 September 2015</div> <div>&#160;</div> <div class="bz"> <p class="cn"><span class="cl">Marks &amp; Spencer Group PLC</span></p> <p class="cn"><span class="cl">&#160;</span></p> <p class="cn"><span class="cl">TRANSACTIONS IN OWN SECURITIES</span></p> <p class="a"><span class="cj">&#160;</span></p> <p class="a"><span class="cj">&#160;</span></p> <p class="a"><span class="cj">&#160;</span></p> <p class="co"><span class="ci">Marks and Spencer Group plc</span><span class="cj"> (</span><span class="cp">"</span><span class="ci">Marks &amp; Spencer</span><span class="cj">") announces today that it has purchased the following number of its ordinary shares of 25 pence each on the London Stock Exchange from </span><span class="ci">Morgan Stanley &amp; Co. International PLC. Such purchase was effected pursuant to irrevocable instructions issued by </span><span class="ci">Marks &amp; Spencer </span><span class="ci">on 7 July 2015, as announced on 7 July 2015</span><span class="ci">:</span></p> <p class="co"><span class="ci">&#160;</span></p> <p class="cq"><span class="cf">Ordinary Shares</span></p> <p class="co"><span class="ci">&#160;</span></p> <p class="co"><span class="ci">Date of purchase:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 8</span><span class="ci"> September 2015</span></p> <p class="co"><span class="ci">&#160;</span></p> <p class="co"><span class="ci">Number of ordinary shares purchased:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 139,705</span></p> <p class="co"><span class="ci">&#160;</span></p> <p class="co"><span class="ci">Price per share:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 508.89p</span></p> <p class="co"><span class="ci">&#160;</span></p> <p class="co"><span class="ci">Marks &amp; Spencer </span><span class="ci">intends to cancel the purchased shares.</span></p> <p class="co"><span class="ci">&#160;</span></p> <p class="co"><span class="ci">Since </span><span class="ci">8 July 2015</span><span class="cj">, </span><span class="ci">Marks &amp; Spencer </span><span class="ci">has purchased 5,821,474 shares at a cost (including dealing and associated costs) of &#163;30,696,909.</span></p> <p class="co"><span class="ci">&#160;</span></p> <p class="cq"><span class="cf">This announcement does not constitute, or form part of, an offer or any solicitation of an offer for securities in any jurisdiction.</span></p> <p class="cr"><span class="cd">&#160;</span></p> <p class="a"><span class="cj">Contact:<span style="FONT-WEIGHT: bold"><span class="cb">&#160;</span></span></span></p> <p class="a"><span class="cj">&#160;</span></p> <p class="a"><span class="cj">Anthony Clarke </span></p> <p class="a"><span class="cj">Deputy Secretary </span></p> <p class="a"><span class="cj">Marks and Spencer Group plc</span></p> <p class="a"><span class="cj">020 8718 9940 </span></p> <p class="cn"><span class="cl">&#160;</span></p> </div> <div style="text-align:center;"><br/>This information is provided by RNS</div><div style="text-align:center;">The company news service from the London Stock Exchange</div><div>&#160;</div> END<div>&#160;</div><div>&#160;</div>POSLLFVRAFITIIE</body> </html>
]]>
</ht_ann>
<tx_webcast_url nm_prov=""/>
</Announcement>

我正在访问此内容:

        foreach($this->xml->Announcement as $announcement) {
            $this->createPDF($announcement->ht_ann);
        }

2 个答案:

答案 0 :(得分:1)

通过在创建pdf之前将HTML写入文件来修复它:

所以要获得pdfcrowd打印变量$htmlString我可以这样做:

     file_put_contents('xmlrequest.html', $htmlString);
     $this->createPDF('xmlrequest.html');

我还更新了我的createPDF()函数:

     $client->convertFile($htmlContent, $out_file);

而不是:

     $client->convertHTML($htmlContent, $out_file);

我无法找到任何可行的原因以及我尝试的方式。

答案 1 :(得分:0)

我遇到了同样的问题,我通过以下步骤解决了问题:

$content = "<html><body></body></html"; //What ever html you want to send.

$pdf = $client->convertHtml("{$content}", $fd);

感谢。