Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use domPDF with WordPress

So I want to have a button on my WordPress posts that will convert the post to PDF. I found domPDF but I'm not sure how to implement it with WordPress?

I know it will be easier with a plugin, but I want to do it without a plugin.

Can someone please explain to me how I can implement this? Thanks

like image 656
Kierenblade Avatar asked Dec 10 '22 15:12

Kierenblade


1 Answers

You're question is very open ended. Typically Stack Overflow is for asking questions on very specific things, like: "Why does this piece of code not function as expected?" or "Which function should I use to perform XYZ task in [insert programming language name here]".

Having said this - we all occasionally need a bit of a kickstart, when it comes to figuring out process. So - I'll answer your question as best I can; without being too specific (after all, I'm not here to do your task for you ;) ).

Do you REALLY need to code something?: If you're looking to convert a WordPress post to PDF, you might be about to re-invent the wheel. Here are two plugins I found at the top of a Google search (I have never used them, and haven't fully read their documentation):

  • https://wordpress.org/plugins/wp-pdf-generator/
  • https://en-gb.wordpress.org/plugins/wp-post-to-pdf-enhanced/

They might do exactly what you need, without having to delve into coding something of your own.

"I just said I didn't want to use a plugin": OK, no problem. But I warn you to tread carefully if this is the case. WordPress uses plugins to extend it's functionality for a number of very good reasons.

  • http://wpninjas.com/stop-adding-code-functions-file/
  • https://premium.wpmudev.org/blog/why-you-shouldnt-use-functions-php

You can add additional functionality through your theme's functions.php file, or create a child theme and add your changes there; however, this has a number of drawbacks for the type of functionality you're looking to add.

Generally, if you're looking to drastically change something (yes, in this case -you are; you're adding something entirely new that WordPress wasn't originally designed to do) - then you should put this into a plugin.

If, on the other hand, your changes were small and only changed default configurations (i.e changing the default sort order of posts inside a loop) - then this would be fine to add to your theme's functions.php file.

To add to this - if you were changing something theme specific, you would also add this to your theme's functions.php. Except - if you didn't write the theme yourself, you should use a plugin or create a child theme.

OK - thanks for the heads up. Now give me the answer. Whichever option you take (writing a plugin, creating a child theme or modifying your main theme) - the core steps to achieve what you need are more or less the same.

Assuming you've familiarised yourself with DomPDF's documentation, you'll know that it's pretty straightforward to get it generating PDF documents:

// EXAMPLE TAKEN FROM https://github.com/dompdf/dompdf
// reference the Dompdf namespace
use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF to Browser
$dompdf->stream();

The piece of code that says $dompdf->loadHtml('hello world'); is where you're going to want to inject your HTML markup.

The easiest option for you at this stage would be to construct a template to pass to DomPDF's loadHtml function. The template would contain the post's title and content.

The code you write will executed from inside the instance of the post you want converted (if this makes no sense to you - I suggest taking a look at the WordPress Codex). Below is an example of something you might consider doing:

// DomPDF will have already been initialised at this point in the code.

// Create a function that DomPDF can reference, for ease.
function grabPost() {

// Hook into loop (don't just copy and paste this. Read the codex!)
global $post;

// Creating a really simple template to pass to DomPDF
$title = '<h1>'.$post->post_title.'</h1>';
$content = '<article>'.$post->post_content.'</article>';

$passing = $title.$content;

return $passing;
}

// Pass our generated template into DomPDF
$dompdf->loadHtml(grabPost());

// Rendering PDF and outputting to browser happens below this line

If you have read through all of this and now think "I'm really confused", I'd advise spending some time brushing up on your knowledge of how WordPress is structured. Take some time and experiment.

I hope this helps :)

like image 189
Alex Mulchinock Avatar answered Dec 24 '22 10:12

Alex Mulchinock