Pdf of content using dompdf
-
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
The topic ‘Pdf of content using dompdf’ is closed to new replies.