Php convert html to pdf using fpdf

Hi

I have a php script that reads in the contents of an existing html file and saves it to a variable called $home_page.

For a test I displayed this by printing variable out and the output displays as expected.

Then using fpdf I wrote this variable out to create a pdf and email it to test to see if it works.

When I receive the pdf via email, I’m getting a msg indicating that my pdf is corrupt.

How do I resolve this?`

    require("fpdf.php");
    $the_file       = "/temp/your-app-test.html";
    $myfile         = fopen($the_file, "r") or die("Unable to open file!!!!<br><br><br>");
    $homepage     = file_get_contents($the_file);
    fclose($myfile);
    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',9);
    $pdf->Cell(40,10, $homepage);

    $pdf->Output();

I doubt a webpage will fit in a 40 by 10 cell. I haven’t worked with fpdf but once before but I would think you would save it as a pdf file and then attach it to your email… not sure. Hey I don’t even know if html is allowed in a cell.

fpdf works with text so whatever is being inserted there would be treated as text not as HTML.

The problem is probably that the content doesn’t fit inside the cell or contains invalid characters.

1 Like

Is the PDF corrupted if you save it locally? That is, can you eliminate the ‘sending by email’ part of the process and just make sure that the PDF is good before it goes anywhere?

Don’t you need to set the filename in Output()?

I believe just using Output() will render it to the screen as PDF file.

Correct, but the OP wants to Email the PDF, so that will require a file if it’s going to be an attachment. The docs say that if you use Output() and put anything after that, things go wrong. So I guess if you use Output() then you are rendering to screen and that’s all.

Agreed. That’s why I mentioned in POST#2 to save as a file then attach it.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.