Generate PDF WordPress Post Without Plugin Dompdf Tutorial

WordPress based CRM project, generate PDF on WordPress Post without plugin, you always needed the dynamic PDF generation feature for the readers for creating PDF downloading option that can help readers to download your post direct and read it later. This tutorial is very important I am also creating one of you can check in https://blog.techboto.com/ after you know full details about this tutorial how it works, is this helpful. You can check our YouTube video check bellow and please Subscribe to our channel for this type of video.

Step – 1 Download domPDF Library

domPDF is available on GitHub and can download it directly and also install using Composer. So I am using download domPDF library at GitHub (https://github.com/dompdf/dompdf/releases), so I recommend just using GitHub to download Dompdf.

Generate PDF WordPress Post Without Plugin Tutorial

Step – 2 Create WordPress Function

Now, this step we can create a function, this is mainly for using our PDF to generate a link, this function can getting details from the database and send it to domPDF that help to create pdf to your WordPress Post or Page, There are two functions.

  • Get database id for particular post and
  • Get post title.
// function that runs when ShortCode
function pdf_download_generate() {  
// Things that you want to do for get post id. 
$pdf_post_id = get_the_ID();        
return $pdf_post_id;
} 
// post id shortcode
add_shortcode('pdfDoload', 'pdf_download_generate'); 
 
function pdf_post_title() {  
// Things that you want to do for get post title and replace. 
$pdf_posttitle = str_replace(" ","-",strtolower(wp_get_document_title()));
return $pdf_posttitle;
}
// post title shortcode
add_shortcode('pdfTitle', 'pdf_post_title');
Generate PDF for WordPress functiontorial

Both functions are run with shortcode, it is important while we can generate pdf link to a perfect place where you want, the perfect place to show download link is sidebar we can use it a widget and show Download button.

Step – 3 Create Download Button for PDF

So, in the step we can create a download button on the sidebar widget, let’s go do this, first log-in your dashboard, go to Appearance → Customize, now you can find sidebar, in some case you can find this option inside another option, because of the different theme has different options.

Or you can simply go to Appearance → Widgets you got the sidebar option.

Create Download button for PDF

Now, inside the sidebar section adding Text (Arbitrary text) section inside the sidebar widget, this widget title name ‘PDF Download’, at the bellow blank text area there is two option one is Visual and Text, we select Text and paste this code simple.

<a class="wp-block-button__link has-background" style="background-color: #ff3d00; border-radius: 1px; color: #fff; width: -webkit-fill-available;" href="/wp/pdf_converter/htmltopdf.php?Tboto=[pdfDoload]&amp;PdfTitle=[pdfTitle]">Download</a>

You can do some modification, add some css style to look good.

Step – 4 Set-up and link domPDF

We already download domPDF file from GitHub, now this file uploads your WordPress hosting server (also you can try this in your local computer), location of the uploading file is in root directory domPDF, main files mention image bellow, create a folder and name it ‘pdf_converter‘ inside this folder you can upload this files (domPFD).

Create a folder name it pdf_converter

Step – 5 domPDF Configuration Database

Inside this pdf_converter folder, there is a file name is ‘htmltopdf.php‘, you can edit and open this file. Now want to know your Database Username, Database Password, Database name, you can find this on your own, Now, simple open directory folder wp-config.php.

Set-up and link with domPDF

You can open your database where your db table, simply open it and find Ctr+f ‘_posts’, now you can copy this name, get a reference from image. Now this paste into wp_posts to your PREFIX_posts.

Generate PDF WordPress Post Without Plugin Tutorial

This bellow code replace inside file name ‘htmltopdf.php‘, also check this image for more details.

<?php
$pdf_getid=$_GET['Tboto'];
// $pdf_getid=$pdf_getid;
$pdf_title=$_GET['PdfTitle'];
require 'vendor/autoload.php';
// Edit Database details hear
$con=mysqli_connect('localhost','db-Username','db-Password','db-table-name');
// Database table name edit PREFIX_posts
$res=mysqli_query($con,"select * from PREFIX_posts where ID='$pdf_getid'");

if(mysqli_num_rows($res)>0){
while($row=mysqli_fetch_assoc($res)){
	 $fulldata_=utf8_decode($row['post_content']);
	 $maintitle_=$row['post_title'];
	 $websitelink=$row['guid'];
$html='<html><head>
<style>
* {	width: auto;  }
p { 
	text-align: left; 
	font-size: 1em; 
	padding: 0px; 
	margin: 12px 0px;
	line-height: normal; 
	font-weight: lighter;
}

.titletxt{
	font-size: 40px;
	text-align: center;
}
.Tb_desi{
	text-align: right; 
	margin-top: 50px;
	font-size: 20px;
	letter-spacing: 1px;
	color: #325c9a;
}
</style>
</head><body>
<div><p class="titletxt">'.$maintitle_.'</p></div>'.$fulldata_.'
<div style="border-radius:3px;font-weight:bold;background: red;margin: 0 auto;" align="center" height="48"><a target="_blank" href="'.$websitelink.'" style="color:#ffffff;text-decoration:none;font-size:16px;display:block;font-family:Helvetica,Arial,sans-serif;padding: 12px 4px;" rel="follow" >Visit Post</a> </div>
<div class="Tb_desi"><span>https://techboto.com</span></div></body></html>';
}
}
else{$html='<center><h2>No PDF found!</h2></center>';}

use Dompdf\Dompdf;
define("DOMPDF_UNICODE_ENABLED", true);
$dompdf= new Dompdf();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4','portrait');
$dompdf->render();
$dompdf->stream("".$pdf_title."",array("Attachment"=>0));

Conclusion:

In this article, we discussed how to easily convert WordPress Post to PDF using domPDF, although domPDF is a great library. Most of the CSS features are working properly and you get completely supported and there is only limited support for CSS3, domPDF is fairly simple and suitable for the majority of PDF export needs.

We have generated WordPress Post to pdf file using PHP and MySQL database, You can generate pdf using another database as well. You just need to create a connection with other types of databases.

Leave a Reply

Your email address will not be published. Required fields are marked *