简体   繁体   English

使用PHP下载PDF文件

[英]PDF file download using PHP

When I click a download button, an ajax request is send and the HtmlToPDF() php function is called. 当我单击下载按钮时,将发送一个ajax请求,并HtmlToPDF() php函数。

First I will create a pdf file from an html file using FPDF. 首先,我将使用FPDF从html文件创建pdf文件。 After that I want to download the pdf file created . 之后,我要下载创建的pdf文件。

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. PDF已创建。 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 ! %PDF-1.3 3 0 obj <> endobj 4 0 obj <>流x YMS 0 W h aɽA ) o,L,Rw ۡÿ l a ! 3 $} Z} R9PDdDD(_)YL〜&$ c3zk {,} 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 Q )< Pg}Q e Χ7W 0e鞯e.e.bbNW6 ] 4%pJWg。 yy.o.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 00000000 C <܅ '' J Y} 1R + 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 o2 2 !! sަc-?v S [f&N.l &A } ^?P 8 x X $ i ryry||gUZ X0= Q^ W ʮGGG^' 8 Ԁ yy d Ӵ~~#$$ h] 3 * * Géeendstream endobj 1 0 obj <endobj 5 0 obj <endobj 6 0 obj <endobj 7 0 obj <endobj 2 0 obj <> endobj 8 0 obj << /生产者(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 00000000 87 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 87 00000 n 0000001003 00000 n 0000001099 00000 n 0000001200 00000 n 0000001412 00000 n 0000001536 00000 n拖车<< / 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 PHP的PDF格式错误

But can't solve my problem. 但是不能解决我的问题。 Please help me.. 请帮我..

According to FPDF documentation, you could use the second Output() argument 根据FPDF文档,您可以使用第二个Output()参数

Destination where to send the document. 文件发送目的地。 It can take one of the following values: 它可以采用以下值之一:

I: send the file inline to the browser. I:将文件内联发送到浏览器。 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. 当选择生成PDF的链接上的“另存为”选项时,将使用按名称提供的名称。

D: send to the browser and force a file download with the name given by name. D:发送到浏览器并使用名称给定的名称强制下载文件。

F: save to a local file with the name given by name (may include a path). F:使用名称给定的名称保存到本地文件(可能包含路径)。

S: return the document as a string. S:以字符串形式返回文档。 name is ignored. 名称将被忽略。

By using the D argument, it should force a download, without using AJAX 通过使用D参数,它应强制下载而不使用AJAX

Source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM