2016-05-31 104 views
0

我想要获得HTML文件的PDF,我正在使用TCPDF生成PDF和AJAX来发送信息。现在,我只是试图在页面上获得一个SVG的PDF,当我完成这些工作时,我会完成剩下的工作。无法使用AJAX下载使用TCPDF生成的PDF

问题是,TCPDF生成的PDF,而不是下载它,它将它传递给JavaScript COOL。当我不使用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 
//============================================================+ 
?> 

在此先感谢您的帮助。

+0

您是否尝试保存pdf并返回保存的文件的URL,而不是返回pdf的内容? – Timothy

+0

@Timothy通过PHP代码上的'Output'方法,保存PDF。它适用于我不使用AJAX的情况。 –

+0

您正在使用输出法D'',这会将文档发送到您的浏览器并开始下载。当你通过AJAX调用收到它时,这不会起作用。 – Timothy

回答

2
$returnValue = $pdf->Output($name, $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中提供有效的路径。

+1

它的工作原理,谢谢。 –

+0

@timothy您提供的文档链接不再有效。你能否提供替代 – Annapurna

+0

@Annapurna https://tcpdf.org/docs/srcdoc/tcpdf/class-TCPDF/#_Output。我似乎无法找到更新的网址,该网址除了提供此答案中已有的信息外,还提供其他信息。我将从我的答案中删除该网址 – Timothy

0

的PDF打印给Javascript控制台因您的线路

console.log(result); 

可能的解决方案:

  • 保存在服务器上的临时文件,PDF和一个URL回到这个文件,您然后分配到location.href
  • 请勿使用Ajax,而应使用<form>
  • 你可以尝试document.write(result)
+0

如果我尝试使用表单,我得到一个错误,说URL太长。 –

+0

也许尝试使用'method =“POST”'而不是'GET',在你的表格中 – Timothy

+0

由于你在php脚本中使用了'$ _POST',所以这很关键。 –

0

我尝试在POST调用中下载PDF时遇到了同样的问题, 尝试直接调用基因PDFPDF页面(使用GET),并且可以正常工作。