Hi this is what I'm getting with the domPDF. Is my final result, but obviously I don't want that, I want my page printed in PDF.
I need the file get downloaded in the user browser, not in localhost.
This is my javascript:
$("#submit").click(function(){
$.post("converter/converter.php", {strHtml: $(".container- fluid").html()}, function(data, status){
alert("Data: " + data);
});
});
this is my php:
<?php
require_once '../dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$html = $_POST['strHtml'];
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->output('test.pdf', 'F');
?>
This is what I want to display in pdf :
If you want to save the generated pdf file to disk. You don't need file_put_content()
. But you pass two parameters to the ->output()
method, like this:
$dompdf->output($path_to_new_file, 'F');
If you want to submit the page as exists in the browser you'll have to capture the content (as you have done) and then, instead of submitting via AJAX request, push that content into a form and submit that form.
$("#submit").click(
function() {
$('#strHtml').val($(".container-fluid").html());
$('#makePdfForm').submit();
}
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class=".container-fluid">
<p>Some stuff here, rendered dynamically on the client</p>
</div>
<form action="converter/converter.php" method="post" id="makePdfForm">
<input name="strHtml" id="strHtml" type="hidden" value="">
<button type="button" id="submit">Make PDF</button>
</form>
Then your PHP changes very little:
<?php
require_once '../dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$html = $_POST['strHtml'];
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream('test.pdf');
?>
</div>