• We have a custom post type displaying products. When a visitor is browsing a particular product, they should click a ‘download pdf’ button that presents them with a pdf with only specific data for the product, it isn’t a replication of the product page on the website.

    I have used dompdf on a non wordpress project with success, however I can’t figure out how to work within WordPress.

    The ‘download pdf’ button is linking to the file that grabs the pdf contents and previews to screen

    <?php
    // website.com/wp-content/themes/themename/test.php
    
    // include autoloader
    require_once 'dompdf/autoload.inc.php'; //the dompdf folder is located /wp-content/themes/themename/dompdf
    
    // reference the Dompdf namespace
    use Dompdf\Dompdf;
    
    // instantiate and use the dompdf class
    $dompdf = new Dompdf();
    
    $html = file_get_contents("pdfcreator.php"); //located at /wp-content/themes/themename/pdfcreator.php
    $dompdf->loadHtml($html);
    
    // (Optional) Setup the paper size and orientation
    $dompdf->setPaper('A4', 'landscape');
    
    // Render the HTML as PDF
    $dompdf->render();
    
    // Output the generated PDF (1 = download and 0 = preview)
    $dompdf->stream("myfile",array("Attachment"=>0));
    
    ?>

    This works well as pdfcreator.php simply contains static placeholder html, such as

    <html>
    <head>
    <style>
    h1 {font-size: 16px}
    </style>
    <body>
    <h1>Title here</h1>
    <p>Description here</p>
    </body>
    </html>
    

    But now I need to pass the actual product info such as title, description, thumbnail and also some advanced custom fields containing further product data.

    If I add

    <?php wp_head();?>

    To the pdfcreator.php file the page simply stops rendering at that point. The error logs on the server are empty.

    Is it possible to use wordpress queries in this page or do you think I’m going to need to pass a lot of info over when creating the pdf such as

    
    $html = file_get_contents("pdfcreator.php?title=product title&description=ds jhfjksfsdf jjsf jksdf jks&thumbnail=thumb.jpg&advancedcustomfielddata1=fhsjdfhjsf sjdhf jshkdfjks&advancedcustomfielddata2=fhsjdfhjsf sjdhf jshkdfjks");
    

    Or is there a much better way of doing this?

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter candell

    (@candell)

    Sorry, do I post this in the functions file?

    :-p

    Moderator bcworkz

    (@bcworkz)

    If you’re going to continue to stream $dompdf output, there’s no way to inject data from WP queries with PHP. It’s possible with JavaScript, but even if you did do this, you’d also need to inject the same data into the download stream for the downloaded file to match the preview. This is just not possible. The data needs to be sent to PDF Creator to begin with so both output types match. Adding the data after the fact is not feasible.

    I would hope PDF Creator would accept POST data as well as GET. It’s much cleaner to send large amounts of data via POST. You’d have to use something like cURL since file_get_contents() doesn’t support POST. If pdfcreator.php is on the same server as WP, you don’t need any transfer protocol, just assign the data to the variables used by pdfcreator.php and include the file as part of your own PHP script.

    If you’re going to use WP functions to collect the data on this page, you need to initialize the WP environment. It can actually be quite simple, but it’s not done by calling wp_head() 🙂 Simple, yes, but your options are limited. The easiest option is to create a custom page template. Add a page based on your template, load this page by its permalink and your code along with PDF Creator executes.

    For the record, the other options are AJAX (not simple) and by going through admin-post.php (simpler but not as easy as page templates) The only reason a page template may not work is if you cannot create a child theme to protect the file from theme updates.

    A variation on this approach is used by the WP PDF Templates plugin. You may find it easier to work with than rolling your own code.

    Thread Starter candell

    (@candell)

    Thank you for a great reply bcworkz, I have gone down the template route and it is working great. Never thought of creating a template just for the pdf.

    Shame about the lack of POST as standard, passing over just the ID using curl to the template page worked superbly.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Pdf of content using dompdf’ is closed to new replies.