This is an archived forum post. The information may be outdated. Contact us if you have any questions.

using convertHtml() in php: Pdfcrowd giving Error: [400] No data to convert. Missing src field.

mikesource wrote on 2015-10-12:
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';

        //GET OUTPUT AS STRING AND PUT IN TO SOME FILE
        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, so this variable definitely contains HTML), and then 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.

Can anyone help?
mikesource wrote on 2015-10-12:
Update: 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?
support wrote on 2015-10-12:
Hello,

That's odd. Could you please dump $htmlContent to a file and send it to us?
mikesource wrote on 2015-10-14:
The HTML gets provided as part of an XML request from London Stock Exchange, which I've saved and attached an example of (had to edit to make smaller than 200kb).

My code filters through these for a specific ID and calls the above function 'createPDF()' like this:

        foreach($this->xml->Announcement as $announcement) {
            // 3. Filter the 20 that came back by cd_tidm field = "AGR";
            if($announcement->attributes()->cd_tidm == 'AGR'){
                $this->createPDF($announcement->ht_ann);
            }
        }


So with the XML request in the file that's attached, you end up with 1 matching announcement, so should be 1 PDF.

The $htmlContent that gets echo'd with the line:
echo "<br>HTML:<br><br>" . $htmlContent . "<br><br><br>";


Is attached as 'htmlstring.html'.

Hope that helps I agree, it is odd, I'd really like to use pdfcrowd as its so far the only pdf converter that's rendered html tables and lists correctly.

Sorry if too much info, just trying to give you everything you might need.

(EDIT: removed long html from post)
mikesource wrote on 2015-10-14:
Update: Fixed by writing the html to file first, no idea why this would work and the way I was doing it doesn't, but there you go...

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 would be interested in knowing why what I was doing didn't work....
support wrote on 2015-10-17:
There should be no difference whether you use convertFile or convertHML.
What happens when you read xmlrequest.html back to $htmlString and pass it to convertHTML()?