无法使用 AJAX 下载 TCPDF 生成的 PDF

Can't download a PDF generated with TCPDF using AJAX

我想获取 HTML 文件的 PDF,我正在使用 TCPDF 生成 PDF 并使用 AJAX 发送信息。现在,我只想在页面上获取一个 SVG 的 PDF,完成后我会做剩下的。

问题是 TCPDF 生成了 PDF,但不是下载它,而是将它传递给 javascript 控制台。当我不使用 AJAX 时,它工作得很好。

这是 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);
    }
});

这里是 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
//============================================================+
?>

在此先感谢您的帮助。

由于您的线路

,pdf 已打印到Javascript 控制台
console.log(result);

可能的解决方案:

  • 将 pdf 保存在服务器上的临时文件中,然后 return 将 URL 保存到此文件,然后将其分配给 location.href.
  • 不要使用 Ajax,而是使用 <form>
  • 你可以试试document.write(result)
$returnValue = $pdf->Output($name, $destination);

$destination 可以采用以下值之一:

I: send the file inline to the browser (default). The name given by name is used when one selects the "Save as" option on the link generating the PDF.

D: send to the browser and force a file download with the name given by name.

F: save to a local server file with the name given by name.

S: return the document as a string (name is ignored).

FI: equivalent to F + I option

FD: equivalent to F + D option

E: return the document as base64 mime multi-part email attachment (RFC 2045)

错误Unable to create output file表示TCPDF无法将您的文档写入您的文件夹。在 $name.

中提供有效路径

我在 POST 通话中尝试下载 PDF 时遇到了同样的问题, 尝试直接调用 generarPDF.php 页面(使用 GET)并且会正常工作。