Question

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.

This is my 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();

edit1 can anyone help?

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 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?

Was it helpful?

Solution

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

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);

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top