Export pdf using php with image support

Export pdf using php with image support

composer require dompdf/dompdf
<?php

require 'vendor/autoload.php';

use Dompdf\Dompdf;

// Initialize dompdf
$dompdf = new Dompdf();

// Load HTML content
$html = '<h1>Export HTML to PDF</h1><p>This PDF is generated using Dompdf.</p>';
$dompdf->loadHtml($html);

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

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

// Output the PDF (force download)
$dompdf->stream('document.pdf', ["Attachment" => true]);
?>
<?php
require 'vendor/autoload.php';

use Dompdf\Dompdf;
use Dompdf\Options;

// Setup Dompdf
$options = new Options();
$options->set('isRemoteEnabled', true); // Enable loading of remote content
$dompdf = new Dompdf($options);

// HTML content with full image URL
$html = '
<h1>Welcome</h1>
<img src="https://via.placeholder.com/300x100" alt="Sample Image">
<p>This PDF includes an image.</p>
';

$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream('document.pdf', ["Attachment" => true]);
?>
<?php

// Template Name: Certificate Template
// get_header();
// wp_head();

?>


<script src="https://cdn.jsdelivr.net/npm/html2canvas@1.4.1/dist/html2canvas.min.js"></script>

<script>
window.addEventListener('load', function() {
captureAndDownload()
});
</script>


<script>
function captureAndDownload() {
const element = document.getElementById('capture');

html2canvas(document.body, {
windowWidth: document.documentElement.scrollWidth,
windowHeight: document.documentElement.scrollHeight,
scrollX: 0,
scrollY: -window.scrollY,
}).then(canvas => {
// Convert canvas to Data URL
const image = canvas.toDataURL('image/png');

// Create a link element
const link = document.createElement('a');
link.href = image;
link.download = 'capture.png'; // File name

// Trigger download
link.click();
});
}
</script>

<?php
require 'vendor/autoload.php';

use Dompdf\Dompdf;

// Initialize dompdf
$dompdf = new Dompdf();

?>

<?php

global $wpdb;
$id = $_GET['id'];

$result = $wpdb->get_results("SELECT * FROM wp_my_custom_certificate_create_table WHERE id=$id");
foreach ($result as $key => $print) { 
$name = $print->name;
$standard = $print->standard;
$school_name = $print->school_name;
}

?>


<div id="capture">

<div id="certificate_container">
<div style="" id="bgImgForCertificatepk">
<center>
<img src="https://vithaisocial.org/wp-content/uploads/2025/04/Vithai-Somnath-Certificate-A-1.png" style="height:700px; width:100%" />
</center>
</div>
</div>


<div id="total">
<h3 class="names" style="margin-top:10px"><?php echo $name; ?></h3>
<h3 class="school_name"><span style="margin-left:-30px;"><?php echo $standard; ?></span>
<span style="margin-left:230px;"><?php echo $school_name; ?></span></h3>
</div>

</div>

<style>

.school_name{
margin-top:-0px;
}

#total{
margin-top:-53vh;
position:relative;
z-index:100;
margin-left:31vw;
}

#totalpk{
margin-top:-90vh;
position:relative;
z-index:100;
margin-left:31vw;
}

#bgImgForCertificate{
background:url('https://vithaisocial.org/wp-content/uploads/2025/04/Vithai-Somnath-Certificate-A-1.png');
height:170vh;
background-position: center; /* Center the image */
background-repeat: no-repeat; /* Do not repeat the image */
background-size: cover;
}

#certificate_container{
padding:0px;
}

</style>


<?php

// get_footer();
// wp_footer();

?>

 

 

 

 

 

Leave a Reply