Lorsque je clique sur un bouton de téléchargement, une requête ajax est envoyée et la fonction php HtmlToPDF()
est appelée.Téléchargement de fichier PDF en utilisant PHP
D'abord je vais créer un fichier pdf à partir d'un fichier html en utilisant FPDF. Après cela, je veux télécharger le fichier pdf créé.
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;
}
}
Le PDF est créé. mais je ne peux pas télécharger le fichier. la réponse est comme
% PDF-1,3 3 0 obj <> endobj 4 0 obj <> courant xYMS0WhaɽA) o L, Rw ÿ l a ! 3 $ } z } R9 P Dd D D ( _) YL ~ & $ c 3 zk {, } 4pI4 倒 D D> B m-? 49 | ] 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 # > < f0 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 * * Ge 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 < </Producteur (FPDF 1.52) /Creator (HTML2FPDF >>http://html2fpdf.sf.net) /CreationDate (D: 20130903060123) > endobj 9 0 obj < </Type/Catalogue /Pages 1 0 R /OpenAction [3 0 R/fith null] /PageLayout/onecolumn > endobj xref 0000000000 0 10 65535 f 0000000916 00000 00000 0000001304 n 0000000009 00000 n n 0000000087 00000 00000 n 01003 00000 00000 n 0000001099 n 0000001200 00000 00000 0000001412 n 0000001536 00000 n n remorque < < /Taille 10 /Root 9 0 R /Info 8 0 R > startxref 1639 %% EOF
J'ai vu une question de débordement de pile liée à ceci. PDF format error with PHP
Mais je ne peux pas résoudre mon problème. S'il vous plaît aidez-moi ..
Comment vous récupérez le dossier? –
ajax n'est pas la réponse à toutes les questions –
@Pekka Pouvez-vous s'il vous plaît expliquer ?? – Nandu