0

I work with a number of companies who want bios converted to PDF files for download. The problem is that we cannot find a plugin solution that fits our needs. WP-MPDF has worked in the past, but it is deprecated and does not work with WP Engine (a known issue) where some of our customers host. Essentially, we need a way to create a PDF from a WP template that allows us to exclude certain Advanced Custom Fields and control the overall look of the document. I have considered a workaround by creating a separate Print version with a PDF download on it, but that feels pretty clunky. Any help is appreciated!

1 Answer 1

0

You could create a new endpoint using add_rewrite_endpoint, for example /datasheet/ so your URL would be example.com/product/datasheet.

You can then hook in to template_redirect and check that you are on a product page with this datasheet query var set.

From here, you can then load a separate template which will display the fields you require (and exclude those ACF fields you mention you don't want to display). You can then use PHP's output buffer functions to copy your template into a string, and pass that to a PFP tool such as dompdf.

if (isset($wp_query->query_vars['datasheet']) && is_singular('product')) {
   global $post;

   ob_start();
   include(locate_template('template-parts/product/datasheet.php'));
   $datasheet = ob_get_contents();
   ob_end_clean();

   $dompdf = new Dompdf();
   $dompdf->loadHtml($datasheet);
   $dompdf->stream($post->post_name . '.pdf');

   exit();
}

This is a simplified version of code we use to provide a product datasheet, but should give you something to think about.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.