douxu5233 2013-07-03 10:18
浏览 73

HTML2PDF生成损坏的文件

I'm using HTML2PDF (http://html2pdf.fr/) to generate PDF files from a webpage. However, I'm having a kind of weird error: the generated file can be perfectly shown with the "Preview" Mac tool (or simply touching the space bar when the downloaded PDF file is selected), but when I try to open the file with the Adobe Acrobat Pro, the following error message appears:

Adobe reader could not open "file.pdf" because it is either not a support file type or because the file has been damaged (for example, it was sent as email attachment and was'nt correctly decoded).

Here is how I'm generating the PDF content and the PDF file itself:

$content = '<page backtop="12mm" backbottom="12mm" backleft="8mm" backright="8mm" style="font-size: 10pt;">
                <h1>MyTitle</h1>
                <br>
                <table style="width: 100%;">
                    <tr>
                        <td style="width: 25%;"></td>
                        ....
                    </tr>
                </table>
            </page>';

$html2pdf = Yii::app()->ePdf->HTML2PDF('P','A4','en',true,'utf-8');
$html2pdf->pdf->SetDisplayMode('fullpage');
$html2pdf->WriteHTML($content);
$html2pdf->Output($outputname , EYiiPdf::OUTPUT_TO_DOWNLOAD );

Any idea on what can be happening? Maybe I'm missing something about the format or the file encoding? I've tried different encodings but nothing seems to solve the problem...

Thanks in advance for your time and effort! :)

  • 写回答

1条回答 默认 最新

  • doukuangxun5382 2013-07-03 10:51
    关注

    Solved! After dealing with this a little bit more, I've decided to open the PDF file with a plain text editor, and I've found that the PHP page used to generate the file included by default a header and a footer (I'm using the Yii Framework), which resulted in "printing" the HTML header and footer code in the PDF file... Is anyone ever has the same problem I had, the way to solve it is to open the page's controller file (in 'protected/controllers/myPageController.php') and change the following line:

    $this->render('index');
    

    By this one:

    $this->renderPartial('index');
    

    Everything works as expected now! ^_^

    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了