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.
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.
Here is the AJAX code:
// 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
// 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: Unment 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='', $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.
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.
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.
Here is the AJAX code:
// 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
// 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: Unment 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', $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.
Share Improve this question asked May 31, 2016 at 10:05 José MiguelJosé Miguel 1072 silver badges15 bronze badges 6- Have you tried saving the pdf and returning the URL of the saved file, instead of returning the content of the pdf? – Timothy Commented May 31, 2016 at 10:13
- @Timothy With the 'Output' method on the PHP code it is suposed to save the PDF. It works when I don't use AJAX. – José Miguel Commented May 31, 2016 at 10:20
-
You are using the output method
'D'
, this will send the document to your browser and start a download. This wont work when you receive it through an AJAX call. – Timothy Commented May 31, 2016 at 10:28 - @Timothy and which should I use? it shoud work with 'F', right? – José Miguel Commented May 31, 2016 at 10:29
-
If you really want to use AJAX , the only way is to save the document on your server and return the pdf url
echo $pdf->Output('/path/to/pdf/nuevo.pdf', 'F');
– Timothy Commented May 31, 2016 at 10:31
3 Answers
Reset to default 3$returnValue = $pdf->Output($name, $destination);
$destination can take one of the following values:
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)
The error Unable to create output file
means that TCPDF can't write your document to your folder. Provide a valid path in $name
.
The pdf is printed to the Javascript console due to your line
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
. - Don't use Ajax, but instead a
<form>
. - You could try
document.write(result)
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.