0

I'm trying to save HTML content generated by PHP as a PDF file.

To do this I found FPDF.

My script is as follows:

if(isset($_POST['content_to_save']) && isset($_POST['name_to_save'])){
    $file_name = $_POST['name_to_save'];
    $file_content = $_POST['content_to_save'];
    require('fpdf.php');
    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Helvetica','',12);
    $pdf->Cell(40,10,$file_content);
    $content = $pdf->Output('../my_folder/'.$file_name.'.pdf','F');
}

My two variables ($file_name & $file_content) are set as I want them with no issues and it creates the PDF in the correct location with the correct file name, however the actual content is the HTML in text format rather than the actual rendered HTML.

Edit

I have now started trying to use TCPDF

My code now is as follows:

if(isset($_POST['po_content_to_save']) && isset($_POST['po_name_to_save'])){
    $file_name = $_POST['po_name_to_save'];
    $file_content = $_POST['po_content_to_save'];
    require('TCPDF/tcpdf.php');
    $pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    $pdf->SetFont('helvetica', '', 12);  
    $pdf->AddPage();  
    $pdf->writeHTML($file_content);  
    $pdf->Output('../my_folder/'.$file_name.'.pdf', 'I'); 
}

However, now I get the following error:

TCPDF ERROR: Some data has already been output, can't send PDF file
halfer
  • 19,824
  • 17
  • 99
  • 186
Paddy Hallihan
  • 1,624
  • 3
  • 27
  • 76
  • What do you mean with " _the actual content is the HTML in text format rather than the actual rendered HTML_ "? pdf->Cell(...) doesn't convert html content to a pdf-rendered content. You should use something [like this](https://wkhtmltopdf.org/) – Jack Skeletron May 09 '18 at 10:27
  • @JackSkeletron thanks for your response. What I mean is that I can see the `HTML` tags in my PDF file rather than it rendering tables and images etc,. From reading the documentation briefly on `http://www.fpdf.org/` I though that it should be able to render out the actual tags – Paddy Hallihan May 09 '18 at 10:35
  • To be honest I don't like using libraries, if possible I always prefer to code it from scratch myself. I'd much rather a simple solution similar to `file_put_contents('../my_folder/'.$file_name.'.pdf', $file_content, FILE_APPEND);` – Paddy Hallihan May 09 '18 at 10:43
  • There're several PHP libraries with that feature but not FPDF, unless you install some of the optional plugins listed on the page. – Álvaro González May 09 '18 at 10:45
  • @ÁlvaroGonzález thanks for your comment. I'm just after trying some of the additional scripts listed on that page but got a load of errors so don't think they're that trustworthy. If you know of any simple libraries that will do this without having to install anything on a shared server that would be great. – Paddy Hallihan May 09 '18 at 11:08
  • FPDF is awesome but it's very outdated so it'll possibly display warnings in latest PHP versions. You may want to try FPDF's most famous fork: TCPDF. – Álvaro González May 09 '18 at 11:45
  • "TCPDF ERROR: Some data has already been output, can't send PDF file" — Essentially the same as this problem: https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Quentin May 09 '18 at 13:05

1 Answers1

0

So after alot of trial and error with a few different libraries I finally found Dompdf

The following code now does what I was trying to achieve:

require '../vendor/autoload.php';
use Dompdf\Dompdf;

if(isset($_POST['po_content_to_save']) && isset($_POST['po_name_to_save'])){
    $file_name = $_POST['po_name_to_save'];
    $file_content = $_POST['po_content_to_save'];

    $document = new Dompdf();
    $document->loadHtml($file_content);
    $document->setPaper('A4', 'portrait');
    $document->render();
    $output = $document->output();
    file_put_contents('../my_folder/'.$file_name.'.pdf', $output);
}
Paddy Hallihan
  • 1,624
  • 3
  • 27
  • 76