Reputation: 3126
When I click a download button, an ajax request is send and the HtmlToPDF()
php function is called.
First I will create a pdf file from an html file using FPDF. After that I want to download the pdf file created .
Code:
function HtmlToPDF()
{
$fileName = $_POST['fileName'];
$file = basename($fileName, ".html");
$pdf=new HTML2FPDF();
$pdf->AddPage();
$fp = fopen("../".$fileName,"r");
$strContent = fread($fp, filesize("../".$fileName));
fclose($fp);
$pdf->WriteHTML($strContent);
$fullPath = "../Bills/".$file.".pdf";
$pdf->Output($fullPath);
if (is_readable ($fullPath)) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
header('Content-Description: File Transfer');
header("Content-type: application/pdf"); // add here more headers for diff. extensions
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
header('Content-Transfer-Encoding: binary'); // use 'attachment' to force a download
header('Expires: 0');
header("Content-length: $fsize");
header('Pragma: public');
header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
ob_clean();
flush();
echo file_get_contents($fullPath);
exit;
}
}
The PDF is created. but I can't download the file. the response is like
%PDF-1.3 3 0 obj <> endobj 4 0 obj <> stream x��YMS�0��W���h�aɽA��)�o�L,Rw�ۡÿ�l��a��!3�$��}z��}R9�P����Dd�D��D��(��_)YL~�&�$�c�3�zk{,�}�4pI4倒D �D>���B�� ���m-?4۹ ��|�]7Ym�#
H���Tc�8��ӹ!�9�C�L�}?O�O� � � � ��H�,���i�|��G�Q�S���?#R GYB�KR�1�Qҡq��)��Ql�m�% {�/�_�c��XOۆQ
�����)<�Pg}Q��e�Χ7W��ɠ�����0e韉�e�.Βb��N�W �6�] 4�%�pJ�Wg��⡞ƕ�yw �o�'��z�9��V� R��K#��>���<f��r�6� � r|:�OEbfn�ia�]S��! C<܅���"���J�� Y}���1R������+2��b�e��0Z�4H������i����g\h� %R��p�0��c����ڵ�
�@ulW϶���Q�VJz��Vȋ5��J�!����4:��p��@|0�!��B�՚{ñ����X?[F([�pl��G�nHn�4��Q�o 2�����2�!�sަc-�?v�� S[�f&N�l� &A���}^?P�8���x��X��$���i��ͳry��|gUZ�����X0=�Q^�W�Gʮ�G^'�8���Ԁ���ǯy���d��Ӵ�~�#�$h]3*������*��G�e� endstream endobj 1 0 obj < endobj 5 0 obj < endobj 6 0 obj < endobj 7 0 obj < endobj 2 0 obj < > endobj 8 0 obj << /Producer (FPDF 1.52) /Creator (HTML2FPDF >> http://html2fpdf.sf.net) /CreationDate (D:20130903060123) > endobj 9 0 obj << /Type /Catalog /Pages 1 0 R /OpenAction [3 0 R /FitH null] /PageLayout /OneColumn > endobj xref 0 10 0000000000 65535 f 0000000916 00000 n 0000001304 00000 n 0000000009 00000 n 0000000087 00000 n 0000001003 00000 n 0000001099 00000 n 0000001200 00000 n 0000001412 00000 n 0000001536 00000 n trailer << /Size 10 /Root 9 0 R /Info 8 0 R > startxref 1639 %%EOF
I saw some stack overflow question related to this. PDF format error with PHP
But can't solve my problem. Please help me..
Upvotes: 1
Views: 3415
Reputation: 330
According to FPDF documentation, you could use the second Output()
argument
Destination where to send the document. It can take one of the following values:
I: send the file inline to the browser. The plug-in is used if available. 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 file with the name given by name (may include a path).
S: return the document as a string. name is ignored.
By using the D
argument, it should force a download, without using AJAX
Upvotes: 1