Last active
August 15, 2022 05:08
-
-
Save julian-stark/f70e4dd6f3ac0a906aaca3afb605fd75 to your computer and use it in GitHub Desktop.
Use DOMPDF to generate PDF on specific WordPress Post URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Dompdf\Dompdf; | |
use Dompdf\Options; | |
add_action('wp', 'sd_generate_pdf'); | |
function sd_generate_pdf() { | |
// Only on post with id 750 | |
if( get_the_ID() != 750 ) { | |
return; | |
} | |
// Include Dompdf | |
$dompdf_path = ABSPATH . 'wp-content/plugins/pdftest/includes/includes/dompdf/autoload.inc.php'; | |
//echo $dompdf_path; | |
require_once $dompdf_path; | |
// instantiate and use the dompdf class | |
$options = new Options(); | |
// external images | |
$options->set('isRemoteEnabled', true); | |
$dompdf = new Dompdf($options); | |
// get post_id via GET | |
$post_id = $_GET['id']; | |
ob_start(); | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Page Title</title> | |
<style> | |
@page { | |
size: a4 portrait; | |
margin: 0; | |
padding: 0; | |
border: none; | |
} | |
body { | |
font-family: Arial,Verdana,sans-serif; | |
font-size: 33px; | |
text-align: center; | |
border: none; | |
} | |
</style> | |
</head> | |
<body> | |
<!-- content here --> | |
</body> | |
</html> | |
<?php | |
$dompdf->loadHtml( ob_get_clean() ); | |
// (Optional) Setup the paper size and orientation | |
$dompdf->setPaper('A4', 'portrait'); | |
// Render the HTML as PDF | |
$dompdf->render(); | |
// Output the generated PDF to Browser | |
$dompdf->stream(); | |
die; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment