2016-05-31 69 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),並且可以正常工作。