January 8, 2014 3:12 pm

How to Convert HTML to PDF in PHP with fpdf

HTML to PDF conversion is always a problem for PHP Programmers and all the time they search for suitable solutions so after reviewing this article you will not take more than 10 minutes to configure HTML to PDF, I have used a library fpdf open source and very useful library for developers here is a simple tutorial on how to convert How to Convert HTML to PDF with fpdf.

convert-html-to-pdf

You have to download fpdf library and  include it in your PHP file below settings and how to show tags, fonts and images in your pdf file. With fpdf library we used HTMLparser library contributed by programmers and all other libraries available here you can download and use as per your requirement.

index.html

In this file I have created a simple contact form data on submit it show that submitted data on PDF format:

actionpdf.php

This file contain PHP code to generate pdf file and show your submitted data on that file.

In this file we add page and auto page break true if your content increase single page area then it will automatically add 2nd page and process.

These lines used to add a logo and select font size for heading.

Select small font then heading for inner content.

Write HTML to pdf file and output that file on the web browser.

Support

If you need any help regarding its configuration please feel free to comment we love to help you.

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.


Tutorial Categories:
  • atul kumar sharma

    to good

  • Sudhir Gupta

    unable to download

  • Shahbaz Ahmed Bhatti

    can php gang plz start some tutorial on funtion to add edit delete record using call function, so i can understood the function use in html to pdf demo file
    thanks

  • skouta

    Thanks for this scrip. amazing,
    But why this script did not support the Arabic language ?

    • huzoorbux

      Use utf-8 encoding headers in your script it will support Arabic.

  • filluup

    unable to download for me also. gave a error “http/1.0 503 service unavailable”

  • Muhammad Sajjad

    also subscribe to it but unable to download, plz reply solution

    • huzoorbux

      List updated please try now.

      • I also subscribe to it but unable to download, plz reply

  • Mohammed

    Thanks for this scrip
    but the output display
    FPDF error: Some data has already been output, can’t send PDF file

  • Abhishek

    where to find writeHTML.php ? i am a newbeee..?

  • binil billu

    Hi,

    How to generate the pdf with file name ex:samplereport.pdf

  • raj

    good script..

  • Yongz

    also subscribe to it but unable to download, plz reply solution

  • alexc

    I also can’t download the code even subscribed.

  • I also can’t download the code

  • Anwar

    how can i do this in cakephp….plz help me…thanks in advance.

  • PANKAJ KUMAR SINHA

    i have subscribed but unable to download … please help ….

  • klyptonic

    same…unable to download after subscribing…it still says no email found after subscribing three days ago…please help

  • GeoStil

    Hi, I have subscribed, unable to dowlnoad… Can you help me please ?

  • Raj

    hi sir how i implement the external css in pdf. b/c i want to create my own design on pdf page ?

  • Thanks for code

  • jack

    I cant download the code even I already completed subscription
    plz help

  • Pranab

    amazing tutorial !!! i have some problem .. please tell me how to increase width each cell when pdf is generated ? Thanks.

  • Kasia D’Aquino

    Hi,

    I get the following errors:

    Warning: Division by zero in /convert-pdf/WriteHTML.php on line 323
    FPDF error: Some data has already been output, can’t send PDF file

    Any ideas??

    • Kasia D’Aquino

      Ok I’ve figured out it’s something to do with some other code on my page.

  • Kasia D’Aquino

    How do you save to pdf instead of displaying in browser?

    • Lamjing Khomba

      put the following code at bottom of your page
      header(“Content-type:application/pdf”);

      // It will be called downloaded.pdf
      header(“Content-Disposition:attachment;filename=’filename.pdf'”);

  • Crown Angel

    hello i have problem with utf-8 how i can edit to change lang arabic ..

  • Lionheart

    Even after subscribing i cant down, i still cant download

  • Zalakpatel

    Subscribed, but not able to download

  • Antonio Leonardo

    Excelent tutorial, I’m curious to apply!

  • Sonia

    thanx 🙂

  • Sonia

    thanx 🙂

  • mark

    how to change the size of the page??

  • how can i write this 0) { ?> to the variable $htmlTable

    • jagdish sonic

      how to pdf view it prolems

  • Adstyle

    hello, how can i resize the column table in that php? i have tried width=”50%” but it didnt work

  • prakash

    hi, img tag can not convert into pdf

  • sivaraman

    How to increase the TD width..Output is look like shrinked table

  • Aaminan

    How to convert whole html file into pdf ?

  • Allen

    hi, how can i set colspan for TD in TABLE

  • jeyakanth

    hello sir i have a error

    Class ‘PDF_HTML’ not found in E:xampphtdocspdfactionpdf.php on line 3

    • Aakash Sardana

      how can i conver whole html page in pdf format without refreshing the page

  • Harish Kumar

    Class ‘PDF_HTML’ not found,

  • Ataul Islam

    This script is running perfectly. Thanks a lot
    Can you help me how can i save the pdf in my local folder instead of showing the pdf in page? Please advice me.

    • rinoj

      $filename=provide any name;
      //eg: $filename= “doc.pdf”;
      $filelocation = “Your folder Location “;
      $fileNL = $filelocation.”/”.$filename;
      $pdf->output($fileNL,’F’);

    • rinoj

      $filename=provide any name;
      //eg: $filename= “doc.pdf”;
      $filelocation = “Your folder Location “;
      $fileNL = $filelocation.”/”.$filename;
      $pdf->output($fileNL,’F’);

  • Benito Montero

    Is Chinese character supported?

    • huzoorbux

      Didn’t tried test it if face any issue let me know.

    • huzoorbux

      Didn’t tried test it if face any issue let me know.

  • James Momoh

    THE IMAGE UPLOADED IN THE FORM IS NOT DISPLAYING IN THE PDF……can anybody help me please help…please help me please

  • James Momoh

    THE IMAGE UPLOADED IN THE FORM IS NOT DISPLAYING IN THE PDF……can anybody help me please help…please help me please…….

  • shahnawaz

    how can i save this pdf file in my database

  • Prashant bhatt

    if me put all code in one file than it’s not working..

  • Lamjing Khomba

    how can i use colspan and rowspan?

  • rinoj

    i have subscribed successfully,but i can’t download it.pls help me..
    Where the file writeHTML.php

    • ADNAN KHALIL

      the same problem is with me

    • ADNAN KHALIL

      the same problem is with me

    • rafat
      • rinoj

        for what?

        • rafat

          for setup pdf library

          • sabari k

            Where is the file??do u got it? plz help me

  • rinoj

    how can i remove the line on top of the every page?

  • Amrendra mourya

    How can we include whole html ??

  • Amrendra mourya

    When i am using two table then this library only create one table and second table will not generate . Have you any solution regarding this issue??

    • rafat

      use variable name different

      $pdf->WriteHTML(“$htmlTable”);

      $pdf->WriteHTML(“$htmlTable2”);

  • Amrendra mourya

    How we can include css in pdf , Because css is not working Please give me solution regarding to this issue ??

  • jagdish sonic

    i have pdf complete but pdf not view my help

  • Chrystopher Pius Ndugo Gaucho

    thank you very much…love this gang

  • anil

    I subscribe with anilmaurya@neotericuk.co.uk and click on activate link also but its saying you are not subs. What’s wrong while download any example

    • See attached image you are allowed to download.

  • swathi

    i like add a table within another table

  • swathi

    how to do colspan and rowspan in actionpdf.php

  • SHABNAM ASLAM

    hi, can u plz help me to do rowspan in the pdf

  • erfan

    Hi , Thanks , It’s great . But how can i user for utf-8 encoding

  • erfan

    Hi , Thanks , It’s great . But how can i use for utf-8 encoding

    • Don’t use UTF-8 encoding. Standard FPDF fonts use ISO-8859-1 or Windows-1252. It is possible to perform a conversion to ISO-8859-1 with utf8_decode(): $str = utf8_decode($str); But some characters such as Euro won’t be translated correctly. If the iconv extension is available, the right way to do it is the following: $str = iconv(‘UTF-8’, ‘windows-1252’, $str);

      source: http://stackoverflow.com/questions/6334134/fpdf-utf-8-encoding-how-to

      • Muhammad Asif

        Deprecated: Function get_magic_quotes_runtime() is deprecated in C:wampwwwconvert-html-to-pdf-with-fpdffpdf.php on line 1054
        FPDF error: Some data has already been output, can’t send PDF file

  • Muhammad Asif

    Deprecated: Function get_magic_quotes_runtime() is deprecated in C:wampwwwconvert-html-to-pdf-with-fpdffpdf.php on line 1054
    FPDF error: Some data has already been output, can’t send PDF file

    Please help

  • katmac23

    HI
    Love your example…
    I have a php script that finds out the current user, queries the database and displays a completed form based on that information.
    When I add this code at the top of the php script to create a pdf (as shown above), I’m getting no output.

    See attached screenshot.

    What did I do wrong?

  • katmac23

    HI
    Love your example…
    I have a php script that finds out the current user, queries the database and displays a completed form based on that information.
    When I add this code at the top of the php script to create a pdf (as shown above), I’m getting no output.

    See attached screenshot.

    What did I do wrong?

  • Rizwan Gill

    I am using following lines of code in

    actionpdf.php

    //$pdf->Output();
    header(“Content-type:application/pdf”);
    // It will be called downloaded.pdf
    header(“Content-Disposition:attachment;filename=doc.pdf”);
    ?>

    means i have commented out put and added lines for download but i am getting error and empty pdf file

    please see attached image and guide me where i am making mistake. Thank you soo much for your favour in advance.

    • Use fpdf builtin functionality to download image.
      send parameter D in output to download pdf $pdf->Output(“D”);

      ref: http://www.fpdf.org/en/doc/output.htm

      • Rizwan Gill

        Hazoor bhai thanks thanks thanks a lot for your prompt reply

        I am getting blank page now by using these lines of code
        $pdf->SetFont(‘Arial’,’B’,6);
        //$pdf->Output();
        $pdf->Output(“D”);
        ?>

        please guide me where i am making mistake. Again thanks for your precious time

      • Rizwan Gill

        Hazoor bhai thanks thanks thanks a lot for your prompt reply

        I am getting blank page now by using these lines of code
        $pdf->SetFont(‘Arial’,’B’,6);
        //$pdf->Output();
        $pdf->Output(“D”);
        ?>

        please guide me where i am making mistake. Again thanks for your precious time

      • Rizwan Gill

        Hazoor bhai thanks thanks thanks a lot for your prompt reply

        I am getting blank page now by using these lines of code
        $pdf->SetFont(‘Arial’,’B’,6);
        //$pdf->Output();
        $pdf->Output(“D”);
        ?>

        please guide me where i am making mistake. Again thanks for your precious time

  • Rizwan Gill

    I am using following lines of code in

    actionpdf.php

    //$pdf->Output();
    header(“Content-type:application/pdf”);
    // It will be called downloaded.pdf
    header(“Content-Disposition:attachment;filename=doc.pdf”);
    ?>

    means i have commented out put and added lines for download but i am getting error and empty pdf file

    please see attached image and guide me where i am making mistake. Thank you soo much for your favour in advance.

  • Varun Kumar Palisetti

    Thanks Bhai . Im learning how to generate invoice generation and converting it into pdf. This article helped me a lot.

  • Varun Kumar Palisetti

    Hi Bhai,tried your code,everything works fine. Now instead of comment section in your form, i tried a “tinymce editor” looks like this. The problem is,whenever i align the text to center. The “ALIGN” in WriteHTML.php is showing error as “Undefined index: ALIGN in WriteHTML.php on line 62”.
    I tried as much as i can to change it from ALIGN to align, did some other silly changes, but none worked out. Help me out here. Dont tell me the entire solution, tell what i have to check.

  • Rhej Padilla

    Fatal error: Class ‘PDF_HTML’ not found.

  • Krunal

    how to download after converting the file

  • Krunal

    how to download after converting the file plz reply fast

    • Mohd Farhan Rizwan

      Use this bro.

  • Krunal

    while i run actionpdf.php then download file but pdf content does not load

  • Dinesh Kumar

    sir, how to clear this? 31-01-2016=’1454194800′ and 31-01-2016=’145426500′ the same values denotes same date .which one is correct?

  • chie

    Hie , i like the script, working fine , however i need your help , if i
    want to send the form on the submit button as a that outcome pdf to
    email(as an attachement). Please help.

  • Carlos M Gomez

    does not let me download, show a message saying i am not registered, but i already confirm from my email

  • Qamar

    I want to design table in actionpdf.php using css. So how can I do?

  • Kim Young

    Hi Huzoor, Are you available for freelance work? I need help to create a dynamic pdf (using php) that meets web accessibility standards (PDF/UA and WCAG 2.0). Is that difficult to do?

    • Aarvi jall

      Hello Kim Young i am a php freelancer, if you have any work please provide me.

  • Shubham Rathod

    Hello,
    i am trying to give 100% width to table by applying rule but however it is not applied in PDF.
    can you please suggest me what to do to give 100% width to table

    Thank you.

  • daywalker

    where have you used index.html in action.php ????

  • Bob

    Why do I get the following error, what am I missing : /

    Fatal error: Call to undefined method FPDF::WriteHTML2()

  • Anil Kumar

    Sir I have included my html file, pdf conversion is well in my case but the same design of html is not appearing in my converted pdf , like no logos, no lines etc. pls help

  • Karthik Saravanan

    how to save this pdf created file,
    i tried with this code
    file_put_contents(“/pdf/file.pdf”, $output);

    • Karthik Saravanan

      i found it,
      $pdf->Output(“pdf/PDF.pdf”, “F”);

  • Raj

    how to send this pdf file after creating that

  • Raj

    how to send Email this PDF file after creating that

  • mbarki chadi

    how to usea whole html page with css ?
    Any help here?