Reputation: 41
First of all sorry for my English.
I'm trying to create a pdf with a portion of html, using jQuery, Ajax and DomPDF.
In my server side I use the next code:
require_once './dompdf/autoload.inc.php';
if(!empty($_GET['pdf'])){
$html=$_GET['pdf'];
# Instanciamos un objeto de la clase DOMPDF.
$mipdf = new Dompdf();
# Definimos el tamaño y orientación del papel que queremos.
# O por defecto cogerá el que está en el fichero de configuración.
$mipdf ->setPaper("A4", "portrait");
# Cargamos el contenido HTML.
$mipdf ->loadHtml(utf8_decode($html));
# Renderizamos el documento PDF.
$mipdf ->render();
# Enviamos el fichero PDF al navegador.
//$mipdf ->stream("Claustro.pdf");
echo base64_encode($mipdf->output());
}
In client side, in jQuery I have:
$("#imprimir").click(function(){
console.log(datos);
$.ajax({
type: "GET",
dataType: 'text',
url: "./librerias/php/funciones.php",
data: {pdf:datos},
success: function(pdf) {
var modalWidth = $(window).width() - 400;
var modalHeight = $(window).height() - 400
var iframeWidth = modalWidth - 20;
var iframeHeight = modalHeight - 20;
$( "#display_dialog").html('<iframe width="' + iframeWidth + '" height="' + iframeHeight + '" src="data:application/pdf;base64,' + pdf + '"></object>');
$( "#display_dialog" ).dialog({
width: modalWidth,
height: modalHeight,
modal: true,
close: function( event, ui ) {
$( "#display_dialog").html("");
}
});
}
});
});//fin imprimir
I have all html code in var "datos" I want in pdf.
To show the iframe I have an hidden div:
<div id="display_dialog"></div>
but I can't get the PDF, The iframe works but no data.
any solution? can anybody help me? thanks to all!
Upvotes: 2
Views: 8848
Reputation: 41
To solve de the problem I made some changes:
In server side:
require 'vendor/autoload.php';
define('UPLOAD_DIR', 'PDFs/');
if(!empty($_POST['pdf'])){
$html=$_POST['pdf'];
@file_put_contents("texto.txt", $html);
$name=str_replace(" ","+",$_POST['nombre']);
$nombre = $name;
# Instanciamos un objeto de la clase DOMPDF.
$options = new Options();
$options->setIsRemoteEnabled(true);
$mipdf = new Dompdf($options);
# Definimos el tamaño y orientación del papel que queremos.
# O por defecto cogerá el que está en el fichero de configuración.
$mipdf ->setPaper("A4", "portrait");
# Cargamos el contenido HTML.
$mipdf ->loadHtml(utf8_decode($html));
# Renderizamos el documento PDF.
$mipdf ->render();
# Enviamos el fichero PDF al navegador.
//$mipdf ->stream("Claustro.pdf");
$pdf=$mipdf->output();
@file_put_contents(UPLOAD_DIR.$nombre.".pdf", $pdf);
echo json_encode("http://regorodri.noip.me/proyecto/librerias/php/".UPLOAD_DIR.$nombre.".pdf");
//}
}
In client side:
$("#imprimir").click(function(){
var name=$("#day").val();
$.ajax({
type: "POST",
dataType: 'text',
dataType: 'json',
url: "./librerias/php/funciones.php",
data: {pdf:datos,nombre:name},
success: function(pdf) {
console.log("url->",pdf);
window.open(pdf, '_blank');
}
});
Upvotes: 2