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

时间:2022-10-08 09:49:42

I want to get a PDF of an HTML file, I am using TCPDF to generate the PDF and AJAX to send the information. 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.

我想获取HTML文件的PDF,我使用TCPDF生成PDF和AJAX来发送信息。现在,我只是试图在页面上获得一个SVG的PDF,当我完成这项工作后,我将完成其余的工作。

The problem is that TCPDF generates the PDF but instead of downloading it, it passes it to the javascript cosole. When I don't use AJAX it works perfect.

问题是TCPDF生成PDF但不是下载它,而是将其传递给javascript cosole。当我不使用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.

在此先感谢您的帮助。

3 个解决方案

#1


2  

$returnValue = $pdf->Output($name, $destination);

$destination can take one of the following values:

$ 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.

I:将文件内联发送到浏览器(默认)。当生成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:保存到名称为name的本地服务器文件。

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. Provide a valid path in $name.

错误无法创建输出文件意味着TCPDF无法将文档写入您的文件夹。在$ name中提供有效路径。

#2


0  

The pdf is printed to the Javascript console due to your line

由于您的行,pdf将打印到Javascript控制台

console.log(result);

Possible solutions:

  • Save the pdf in a temporary file on the server and return a URL to this file which you then assign to location.href.
  • 将pdf保存在服务器上的临时文件中,并将URL返回到此文件,然后将该文件分配给location.href。

  • Don't use Ajax, but instead a <form>.
  • 不要使用Ajax,而是使用

  • You could try document.write(result)
  • 你可以试试document.write(结果)

    #3


    0  

    I had the same problem when tried to download PDF in POST call, try to call you generarPDF.php page directly (with GET) and will work corrcetly.

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

    #1


    2  

    $returnValue = $pdf->Output($name, $destination);
    

    $destination can take one of the following values:

    $ 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.

    I:将文件内联发送到浏览器(默认)。当生成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:保存到名称为name的本地服务器文件。

    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. Provide a valid path in $name.

    错误无法创建输出文件意味着TCPDF无法将文档写入您的文件夹。在$ name中提供有效路径。

    #2


    0  

    The pdf is printed to the Javascript console due to your line

    由于您的行,pdf将打印到Javascript控制台

    console.log(result);
    

    Possible solutions:

    • Save the pdf in a temporary file on the server and return a URL to this file which you then assign to location.href.
    • 将pdf保存在服务器上的临时文件中,并将URL返回到此文件,然后将该文件分配给location.href。

    • Don't use Ajax, but instead a <form>.
    • 不要使用Ajax,而是使用

  • You could try document.write(result)
  • 你可以试试document.write(结果)

    #3


    0  

    I had the same problem when tried to download PDF in POST call, try to call you generarPDF.php page directly (with GET) and will work corrcetly.

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