TCPDF创建损坏的pdf [英] TCPDF creates damaged pdf

查看:163
本文介绍了TCPDF创建损坏的pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TCPDF不断创建我无法打开的损坏的pdf.有什么解决办法吗?我从html表单发布.我不确定我的代码是否错误,但是我测试了tcpdf的示例,它们可以正常工作.

TCPDF keeps creating a damaged pdf that I an unable to open. any solutions? I am posting from an html form. I am unsure if my code is wrong but I tested tcpdf's examples and they work fine.

这是我的php:

ob_start();

require_once('tcpdf.php');

$pdf = & new TCPDF("P","mm","A4",true,"UTF-8",false);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetAutoPageBreak(false);
$pdf->SetMargins(15,20,15);
$pdf->AddPage();
$pdf->SetFont('helvetica','B',12);
$pdf->SetFillColor(255,255,255);

// set image scale factor

$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// IMPORTANT: disable font subsetting to allow users editing the document

$pdf->setFontSubsetting(false);

//data

$pdf->writeHTMLCell(0,0,0,0, "Job Number ". $_POST["jobnum"], $border=0, $ln=0, $fill=false, $reseth=true, $align='', $autopadding=true);
$pdf->writeHTMLCell(0,0,0,5, "Program ". $_POST["program"], $border=0, $ln=0, $fill=false, $reseth=true, $align='', $autopadding=true);
$pdf->writeHTMLCell(0,0,0,10, "Ship Date ". $_POST["shipdate"], $border=0, $ln=0, $fill=false, $reseth=true, $align='', $autopadding=true);
$pdf->writeHTMLCell(0,0,0,15, "Description ". $_POST["description"], $border=0, $ln=0, $fill=false, $reseth=true, $align='', $autopadding=true);
$pdf->writeHTMLCell(0,0,0,20, "Proto Verified By ". $_POST["name"], $border=0, $ln=0, $fill=false, $reseth=true, $align='', $autopadding=true);
$pdf->writeHTMLCell(0,0,0,25, "Additional Notes ". $_POST["notes"], $border=0, $ln=0, $fill=false, $reseth=true, $align='', $autopadding=true);
$pdf->writeHTMLCell(0,0,0,30, "File Name". $_POST["filename1"], $border=0, $ln=0, $fill=false, $reseth=true, $align='', $autopadding=true);
$pdf->writeHTMLCell(0,0,0,35, "Doc Siize ". $_POST["Docsize1"], $border=0, $ln=0, $fill=false, $reseth=true, $align='', $autopadding=true);
$pdf->Cell(35, 5, 'FC ');
$pdf->CheckBox('fc1', 5, true, array(), array(), 'OK');
$pdf->Ln(40);
$pdf->Cell(35, 5, 'DC ');
$pdf->CheckBox('dc1', 5, true, array(), array(), 'OK');
$pdf->Ln(45);
$pdf->Cell(35, 5, 'Flip ');
$pdf->CheckBox('flip1', 5, true, array(), array(), 'OK');
$pdf->Ln(50);
$pdf->writeHTMLCell(0,0,0,55, "Quantity ". $_POST["quantity1"], $border=0, $ln=0, $fill=false, $reseth=true, $align='', $autopadding=true);
$pdf->writeHTMLCell(0,0,0,60, "Flip Quantity ". $_POST["flipqty1"], $border=0, $ln=0,   $fill=false, $reseth=true, $align='', $autopadding=true);

//Close and output PDF document

$pdf->Output('job.pdf', 'D');


ob_clean();

编辑1 有人可以帮忙吗?

edit2 我得出的结论是我的代码没有问题.我测试了一个基本的"hello world",仍然收到无法打开的损坏的pdf文件?我进行了广泛的研究,尽管这个问题似乎很普遍,但没有人收到这个问题的答案.如果有人可以帮助我,那就太好了.

edit2 I have come to the conclusion that there is no problem with my code. I tested a basic "hello world" and still receive a damaged pdf that I am unable to open? I did some extensive research and no one has received an answer to this problem although it seems pretty common. if anyone could help me out that would be awesome.

edit3 我也尝试过"hellow world"的FPDF,但是仍然遇到pdf损坏的问题? Adobe Acrobat Reader是否会成为问题?

edit3 I also tried FPDF with "hellow world" and I still encounter the same issue with a damaged pdf? would adobe acrobat reader be the issue?

推荐答案

您的代码至少存在两个问题,一个或两个都可能导致该问题.

There are at least two issues with your code, one or both of which may be contributing to the problem.

第一个问题是,您不应该尝试通过引用分配新的TCPDF实例.也就是说,您应该删除&"号,以使所讨论的行如下所示:

The first issue is that you shouldn't be trying to assign the new TCPDF instance by reference. That is, you should remove the ampersand such that the line in question looks like this:

$pdf = new TCPDF("P","mm","A4",true,"UTF-8",false);

第二个问题是,通过调用ob_clean(),实际上是在删除$pdf->Output()时创建的(并存储在输出缓冲区中的)输出.如果您确实要显式管理输出缓冲区,请改用ob_end_flush().话虽如此,您可能仍然不需要在代码中管理输出缓冲,因为PHP默认情况下会缓冲所有输出,并在脚本完成后自动将其发送到客户端.

The second issue is that by calling ob_clean() you are actually deleting the very output that was created (and stored in the output buffer) when you called $pdf->Output(). If you really want to manage the output buffer explicitly then use ob_end_flush() instead. Having said that, you probably don't need to manage output buffering in the code anyway, as PHP buffers all output by default and automatically sends it to the client when the script completes.

为记录起见,我复制了您的代码,并在上面进行了两次调整,然后就可以了.

For the record, I copied your code, made the two adjustments above and it worked.

这篇关于TCPDF创建损坏的pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆