[英]Can't download a PDF generated with TCPDF using AJAX
I want to get a PDF of an HTML file, I am using TCPDF to generate the PDF and AJAX to send the information. 我想获取HTML文件的PDF,我正在使用TCPDF生成PDF,并使用AJAX发送信息。 Right now, I am only trying to get a PDF of one SVG on the page, when I get this done I will do the rest.
现在,我只想在页面上获取一个SVG的PDF,当完成此操作后,我将做剩下的事情。
The problem is that TCPDF generates the PDF but instead of downloading it, it passes it to the javascript cosole. 问题在于TCPDF会生成PDF,但不会下载它,而是将其传递给javascript cosole。 When I don't use AJAX it works perfect.
当我不使用AJAX时,它可以完美工作。
Here is the AJAX code: 这是AJAX代码:
// Get the d3js SVG element
var tmp = document.getElementById("elemPD5graf");
var svg = tmp.getElementsByTagName("svg")[0];
// Extract the data as SVG text string
var svg_xml = (new XMLSerializer).serializeToString(svg);
var j = jQuery.noConflict();
var parameters = { datos : svg_xml };
j.ajax({
type: 'POST',
url: 'generarPDF.php',
data : parameters,
success: function(result) {
console.log(result);
}
});
And here is the PHP code: 这是PHP代码:
<?php
// Include the main TCPDF library (search for installation path).
require_once('..\tcpdf\tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 058', PDF_HEADER_STRING);
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// ---------------------------------------------------------
// set font
$pdf->SetFont('helvetica', '', 10);
// add a page
$pdf->AddPage();
// NOTE: Uncomment the following line to rasterize SVG image using the ImageMagick library.
//$pdf->setRasterizeVectorImages(true);
$svg2 = $_POST['datos'];
$pdf->ImageSVG($file='@'.$svg2, $x=15, $y=30, $w='', $h='', $link='http://www.tcpdf.org', $align='', $palign='', $border=1, $fitonpage=false);
// ---------------------------------------------------------
//Close and output PDF document
$pdf->Output('nuevo.pdf', 'D');
//============================================================+
// END OF FILE
//============================================================+
?>
Thanks in advance for the help. 先谢谢您的帮助。
$returnValue = $pdf->Output($name, $destination);
$destination can take one of the following values: $ destination可以采用以下值之一:
I: send the file inline to the browser (default).
I:将文件内联发送到浏览器(默认)。 The name given by name is used when one selects the "Save as" option on the link generating the PDF.
当选择生成PDF的链接上的“另存为”选项时,将使用按名称提供的名称。
D: send to the browser and force a file download with the name given by name.
D:发送到浏览器并使用名称给定的名称强制下载文件。
F: save to a local server file with the name given by name.
F:使用名称给定的名称保存到本地服务器文件。
S: return the document as a string (name is ignored).
S:以字符串形式返回文档(名称将被忽略)。
FI: equivalent to F + I option
FI:相当于F + I选项
FD: equivalent to F + D option
FD:相当于F + D选项
E: return the document as base64 mime multi-part email attachment (RFC 2045)
E:将文档作为base64 mime多部分电子邮件附件返回(RFC 2045)
The error Unable to create output file
means that TCPDF can't write your document to your folder. 错误“
Unable to create output file
意味着TCPDF无法将文档写入文件夹。 Provide a valid path in $name
. 在
$name
提供有效路径。
The pdf is printed to the Javascript console due to your line pdf会根据您的要求打印到Javascript控制台
console.log(result);
Possible solutions: 可能的解决方案:
location.href
. location.href
。 <form>
. <form>
。 document.write(result)
document.write(result)
尝试在POST调用中下载PDF时,我遇到了同样的问题,尝试直接使用GET调用您的GenerarPDF.php页面,并且可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.