Javascript 使用DOMPDF通过jQuery获取PDF
首先为我的英语感到抱歉 我正在尝试使用jQuery、Ajax和DomPDF创建一个包含部分html的pdf 在我的服务器端,我使用以下代码:Javascript 使用DOMPDF通过jQuery获取PDF,javascript,php,jquery,ajax,dompdf,Javascript,Php,Jquery,Ajax,Dompdf,首先为我的英语感到抱歉 我正在尝试使用jQuery、Ajax和DomPDF创建一个包含部分html的pdf 在我的服务器端,我使用以下代码: require_once './dompdf/autoload.inc.php'; if(!empty($_GET['pdf'])){ $html=$_GET['pdf']; # Instanciamos un objeto de la clase DOMPDF. $mipdf = new Dompdf(); # De
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());
}
在客户端,在jQuery中我有:
$("#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
$(“#imprimir”)。单击(函数(){
控制台日志(datos);
$.ajax({
键入:“获取”,
数据类型:“文本”,
url:“./librerias/php/functiones.php”,
数据:{pdf:datos},
成功:功能(pdf){
var modalWidth=$(窗口).width()-400;
var modalHeight=$(窗口).height()-400
变量iframeWidth=modalWidth-20;
变量iframeHeight=modalHeight-20;
$(“#显示对话框”).html(“”);
$(“显示对话框”)。对话框({
宽度:modalWidth,
高度:modalHeight,
莫代尔:是的,
关闭:功能(事件、用户界面){
$(“#显示对话框”).html(“”);
}
});
}
});
});//财政部批准
我有所有的html代码在var“datos”我想在pdf格式
要显示iframe,我有一个隐藏的div:
<div id="display_dialog"></div>
但我无法获取PDF,iframe可以工作但没有数据
有解决办法吗?有人能帮我吗?谢谢大家 为了解决这个问题,我做了一些修改: 在服务器端:
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");
//}
}
在客户端:
$("#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');
}
});