Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Pdfcrowd to convert HTML to PDF in PHP

I've written the following PHP function to try and generate a 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>";
    }

}

When run in a browser, this echos $htmlContent correctly (displaying the HTML I want to convert, from <html> to </html> tag, so this variable definitely contains HTML), and then gives the following error:

Pdfcrowd Error: [400] No data to convert. Missing src field.

Obviously something is wrong, but the src field is not missing as far as I can tell as it echos perfectly fine, right before it's passed to the convertHTML() function. I'm not sure how to proceed as there appears to be no other info or documentation on this error from pdfcrowd.

(Pdfcrowd documentation here)

If I copy and paste out the echo'd html from $htmlContent and put that into the first parameter for convertHTML() the code works... so why can I not pass it in as a variable? Shouldn't it be the same?

Can anyone help?

Note: I'm open to other solutions, but I have tried: html2pdf, fpdf, tcpdf, dompdf & mpdf already. All have different issues rendering the html I need converting (RNS announcements from London Stock Exchange), which can contain tables and bullet points. See my other question. I got good results putting those RNS urls into the pdfcrowd online converter, so looks like it would work if I get round this error...

Example of the XML Feed (see comments), these can contain more than 1 announcement:

<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>

I'm accessing this like:

        foreach($this->xml->Announcement as $announcement) {
            $this->createPDF($announcement->ht_ann);
        }
like image 611
mike-source Avatar asked Feb 07 '26 03:02

mike-source


1 Answers

Fixed it by writing the HTML to file before creating the pdf:

So to get pdfcrowd to print a variable $htmlString I can do:

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

I also updated my createPDF() function to use:

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

Instead of:

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

I couldn't find any reason why this works and the way I was trying didn't.

like image 144
mike-source Avatar answered Feb 12 '26 06:02

mike-source