hi, i’d like to generate a PDF file from a Custom File, but the pdf is not being generated.
this is my code for the custom file_Content:
<a href="#" id="print-btn" class="btn btn-primary">Print PDF</a>
<div id="printable-area">
<p style="color:red;">Test</p>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
$("#print-btn").click(function() {
var printableContent = $("#printable-area").html();
$.ajax({
type: "POST",
url: "print.php",
data: { printableContent: printableContent },
success: function(data) {
console.log("PDF created successfully!");
},
error: function(xhr, status, error) {
console.log("An error occurred while creating PDF.");
}
});
});
});
</script>
an this is the code for print.php that’s situated in the base project forlder (localost/test/print.php):
<?php
require_once 'vendor/autoload.php';
use Dompdf\Dompdf;
if (isset($_POST['printableContent'])) {
$printableContent = $_POST['printableContent'];
ob_get_clean();
$dompdf = new Dompdf();
$dompdf->loadHtml($printableContent);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream();
}
?>
the console log that’ive setted give me “PDF created successfully!”
and if i use:
console.log(data);
it gives me some code similare to a pdf structure.i’ve tried to add:
const pdfBlob = new Blob([data], { type: 'application/pdf' });
const pdfUrl = URL.createObjectURL(pdfBlob);
window.open(pdfUrl);
for success, ad it open a blank pdf.
any ide to solve this?
Thanks