Doc Brown
Doc Brown

Reputation: 11

dompdf creates defective pdf on wordpress

I want to create a pdf on my wordpress homepage with dompdf. Local an my computer with xampp there is no problem, all works fine. On wordpress I am using the plugin "PHP Code For Posts". The dompdf folder I copied to the folder of the plugin. The pdf get created but it is defective.

<?php

if(isset($_POST['button']))
{
    require_once("dompdf/dompdf_config.inc.php");
    spl_autoload_register('DOMPDF_autoload');

    function pdf_create($filename, $paper, $orientation, $stream=TRUE)  
    {
        $dompdf = new DOMPDF();
        $dompdf->set_paper($paper, $orientation);
        $dompdf->load_html('TEST');
        $dompdf->render();
        $dompdf->stream($filename.".pdf");
    }

    $filename = 'file_name';
    $dompdf = new DOMPDF();
    pdf_create($filename, 'A4', 'portait');  
}

?>

<form method="post" >
    <p>Test: <input type="text" name="price" /></p>
    <p><input type="submit" name="button" value="PDF" /></p> 
</form>

I hope anybody can help me. I have no idea why it works local and on wordpress it creates a defective file.

UPDATE:

Here is the code when I open the defective file with notepad.

<!DOCTYPE html>

<html lang="de-DE">

<head>


<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="profile" href="http://gmpg.org/xfn/11">

<link rel="pingback" href="http://test-homepage.de/xmlrpc.php">


<!--[if lt IE 9]>
<script src="http://test-homepage.de/wp-content/themes/zerif-pro/js/html5.js"></script>
<link rel="stylesheet" href="http://test-homepage.de/wp-content/themes/zerif-pro/css/ie.css" type="text/css">
<![endif]-->

<title>Test &#8211; test-homepage</title>
<link rel="alternate" type="application/rss+xml" title="test-homepage &raquo; Feed" href="http://test-homepage.de/feed/" />
        <script type="text/javascript">
            window._wpemojiSettings = {"baseUrl":"https:\/\/s.w.org\/images\/core\/emoji\/72x72\/","ext":".png","source":{"concatemoji":"http:\/\/test-homepage.de\/wp-includes\/js\/wp-emoji-release.min.js?ver=4.5.3"}};

Upvotes: 1

Views: 1923

Answers (1)

SaschaM78
SaschaM78

Reputation: 4497

This may be a solution if you want to stick to the currently used Wordpress plugin and output buffering is enabled (both webserver and php.ini may control output buffering; see the output buffering section on the PHP.net page for more details.

<?php
if(isset($_POST['button']))
{
    // make sure that output buffering has been enabled
    if (ob_start()) {
        // clear previous contents from the server output cache
        ob_clean();
        try {
            require_once("dompdf/dompdf_config.inc.php");
            spl_autoload_register('DOMPDF_autoload');

            $filename = 'file_name';
            $paper= 'A4';
            $orientation= 'portrait';

            $dompdf = new DOMPDF();
            $dompdf->set_paper($paper, $orientation);
            $dompdf->load_html('TEST');
            $dompdf->render();
            $dompdf->stream($filename.".pdf");

            // make sure the content of the buffer is sent to the client.
            ob_flush();
            // terminate the script to make sure no additional content is sent.
            exit(0);
        } catch (Exception $ex) {
            error_log(sprintf("PDF generation failed with %s in line %d", $ex->getMessage(), $ex->getLine()));
        }
    } else {
        // log a message in default error log
        error_log("Output buffering could not be enabled (PDF routine)");
    }
}

?>

Debugging your setup

As you said you are pretty new to PHP, please create a file with the content:

<?php
phpinfo();

Upload the file to your server and open the file with your browser, scroll down until you find "Configuration" > "PHP Core". In the list below, search for the entry "output_buffering". If it is set to "0", output buffering has been disabled via php.ini. If it has been disabled, all output of your page is directly send to the client and there is no programmatic way to do what you achieve without changing the server setup.

enter image description here

There may be several ways to enable buffering, i.e. your provider may allow changing certain settings via web configuration software or you may add an .htaccess file to your web directory to enable it as described here.


Another update

As you already found out, output buffering has been enabled properly, so we should be able to modify the output buffer to create the PDF even if output had been served before. I copied your code, ran the PHP part after some output had already been sent to the browser and the same happened as on your web page, the static HTML content was also included in my PDF document. The main problem will be that Wordpress does not use output buffering internally and sends the output directly to the browser.

Here's what I did that resolved the problem -> start the output buffering as soon as possible, inside your index.php:

<?php ob_start(); ?>

After buffering had been enabled, I was able to control the buffer and generate the PDF file as required.


Further diagnosis

I just want to make sure we are not missing something, could you add this to your PDF generation code:

if(isset($_POST['button']))
{
  $sent = headers_sent($file, $line);
  die("The output started in {$file} and at line {$line}");
}

Upvotes: 1

Related Questions