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.
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:
<html> <head> <link href="css/bootstrap.min.css" rel="stylesheet" media="screen"> <link href="css/bootstrap-responsive.min.css" rel="stylesheet" media="screen"> <script type="text/javascript" src="js/jquery-1.8.0.min.js"></script> <script type="text/javascript" src="js/bootstrap.min.js"></script> <title>How to create Contact Form using Bootstrap | PGPGang.com</title> </head> <body> <h2>How to create Contact Form using Bootstrap example. => <a href="https://www.phpgang.com/">Home</a> | <a href="http://demo.phpgang.com/">More Demos</a></h2> <div class="container"> <form class="contact-us form-horizontal" action="actionpdf.php" method="post"> <legend>Fill Form and submit to generate PDF</legend> <div class="control-group"> <label class="control-label">Name</label> <div class="controls"> <div class="input-prepend"> <span class="add-on"><i class="icon-user"></i></span> <input type="text" class="input-xlarge" name="name" placeholder="Name"> </div> </div> </div> <div class="control-group"> <label class="control-label">Email</label> <div class="controls"> <div class="input-prepend"> <span class="add-on"><i class="icon-envelope"></i></span> <input type="text" class="input-xlarge" name="email" placeholder="Email"> </div> </div> </div> <div class="control-group"> <label class="control-label">Url</label> <div class="controls"> <div class="input-prepend"> <span class="add-on"><i class="icon-globe"></i></span> <input type="text" id="url" class="input-xlarge" name="url" placeholder="http://www.example.com"> </div> </div> </div> <div class="control-group"> <label class="control-label">Comment</label> <div class="controls"> <div class="input-prepend"> <span class="add-on"><i class="icon-pencil"></i></span> <textarea name="comment" class="span4" rows="4" cols="80" placeholder="Comment (Max 200 characters)"></textarea> </div> </div> </div> <div class="control-group"> <div class="controls"> <button type="submit" class="btn btn-primary">Submit</button> <button type="button" class="btn">Cancel</button> </div> </div> </form> </div> </body> </html>
actionpdf.php
This file contain PHP code to generate pdf file and show your submitted data on that file.
<?php require('WriteHTML.php'); $pdf=new PDF_HTML(); $pdf->AliasNbPages(); $pdf->SetAutoPageBreak(true, 15); $pdf->AddPage(); $pdf->Image('logo.png',18,13,33); $pdf->SetFont('Arial','B',14); $pdf->WriteHTML('<para><h1>PHPGang Programming Blog, Tutorials, jQuery, Ajax, PHP, MySQL and Demos</h1><br> Website: <u>www.phpgang.com</u></para><br><br>How to Convert HTML to PDF with fpdf example'); $pdf->SetFont('Arial','B',7); $htmlTable='<TABLE> <TR> <TD>Name:</TD> <TD>'.$_POST['name'].'</TD> </TR> <TR> <TD>Email:</TD> <TD>'.$_POST['email'].'</TD> </TR> <TR> <TD>URl:</TD> <TD>'.$_POST['url'].'</TD> </TR> <TR> <TD>Comment:</TD> <TD>'.$_POST['comment'].'</TD> </TR> </TABLE>'; $pdf->WriteHTML2("<br><br><br>$htmlTable"); $pdf->SetFont('Arial','B',6); $pdf->Output(); ?>
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.
$pdf->Image('logo.png',18,13,33); $pdf->SetFont('Arial','B',14);
These lines used to add a logo and select font size for heading.
$pdf->SetFont('Arial','B',7);
Select small font then heading for inner content.
$pdf->WriteHTML2("<br><br><br>$htmlTable"); $pdf->Output();
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.
Tutorial Categories:
to good
unable to download
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
Thanks for this scrip. amazing,
But why this script did not support the Arabic language ?
Use utf-8 encoding headers in your script it will support Arabic.
unable to download for me also. gave a error “http/1.0 503 service unavailable”
also subscribe to it but unable to download, plz reply solution
List updated please try now.
I also subscribe to it but unable to download, plz reply
Thanks for this scrip
but the output display
FPDF error: Some data has already been output, can’t send PDF file
where to find writeHTML.php ? i am a newbeee..?
Hi,
How to generate the pdf with file name ex:samplereport.pdf
good script..
also subscribe to it but unable to download, plz reply solution
I also can’t download the code even subscribed.
I also can’t download the code
how can i do this in cakephp….plz help me…thanks in advance.
i have subscribed but unable to download … please help ….
same…unable to download after subscribing…it still says no email found after subscribing three days ago…please help
What email you used to subscribe?
[email protected]
Hi, I have subscribed, unable to dowlnoad… Can you help me please ?
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
I cant download the code even I already completed subscription
plz help
amazing tutorial !!! i have some problem .. please tell me how to increase width each cell when pdf is generated ? Thanks.
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??
Ok I’ve figured out it’s something to do with some other code on my page.
How do you save to pdf instead of displaying in browser?
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'”);
hello i have problem with utf-8 how i can edit to change lang arabic ..
Even after subscribing i cant down, i still cant download
Subscribed, but not able to download
Excelent tutorial, I’m curious to apply!
thanx 🙂
thanx 🙂
how to change the size of the page??
how can i write this 0) { ?> to the variable $htmlTable
how to pdf view it prolems
hello, how can i resize the column table in that php? i have tried width=”50%” but it didnt work
hi, img tag can not convert into pdf
How to increase the TD width..Output is look like shrinked table
How to convert whole html file into pdf ?
hi, how can i set colspan for TD in TABLE
hello sir i have a error
Class ‘PDF_HTML’ not found in E:xampphtdocspdfactionpdf.php on line 3
how can i conver whole html page in pdf format without refreshing the page
Class ‘PDF_HTML’ not found,
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.
$filename=provide any name;
//eg: $filename= “doc.pdf”;
$filelocation = “Your folder Location “;
$fileNL = $filelocation.”/”.$filename;
$pdf->output($fileNL,’F’);
$filename=provide any name;
//eg: $filename= “doc.pdf”;
$filelocation = “Your folder Location “;
$fileNL = $filelocation.”/”.$filename;
$pdf->output($fileNL,’F’);
Is Chinese character supported?
Didn’t tried test it if face any issue let me know.
Didn’t tried test it if face any issue let me know.
THE IMAGE UPLOADED IN THE FORM IS NOT DISPLAYING IN THE PDF……can anybody help me please help…please help me please
THE IMAGE UPLOADED IN THE FORM IS NOT DISPLAYING IN THE PDF……can anybody help me please help…please help me please…….
how can i save this pdf file in my database
if me put all code in one file than it’s not working..
how can i use colspan and rowspan?
i have subscribed successfully,but i can’t download it.pls help me..
Where the file writeHTML.php
the same problem is with me
the same problem is with me
[email protected]
for what?
for setup pdf library
Where is the file??do u got it? plz help me
how can i remove the line on top of the every page?
How can we include whole html ??
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??
use variable name different
$pdf->WriteHTML(“$htmlTable”);
$pdf->WriteHTML(“$htmlTable2”);
How we can include css in pdf , Because css is not working Please give me solution regarding to this issue ??
i have pdf complete but pdf not view my help
thank you very much…love this gang
I subscribe with [email protected] 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.
i like add a table within another table
how to do colspan and rowspan in actionpdf.php
hi, can u plz help me to do rowspan in the pdf
Month
Savings
Savings for holiday!
January
$100
$50
February
$80
Ref: http://www.w3schools.com/tags/att_td_rowspan.asp
thanks for the reply, sir. but how to do table rowspan in actionpdf.php
Hi , Thanks , It’s great . But how can i user for utf-8 encoding
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
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
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
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?
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?
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
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
Update fpdf version http://www.fpdf.org/en/script/index.php
Update fpdf version http://www.fpdf.org/en/script/index.php
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
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
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.
Thanks Bhai . Im learning how to generate invoice generation and converting it into pdf. This article helped me a lot.
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.
Fatal error: Class ‘PDF_HTML’ not found.
how to download after converting the file
how to download after converting the file plz reply fast
Use this bro.
while i run actionpdf.php then download file but pdf content does not load
sir, how to clear this? 31-01-2016=’1454194800′ and 31-01-2016=’145426500′ the same values denotes same date .which one is correct?
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.
does not let me download, show a message saying i am not registered, but i already confirm from my email
I want to design table in actionpdf.php using css. So how can I do?
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?
Hello Kim Young i am a php freelancer, if you have any work please provide me.
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.
where have you used index.html in action.php ????
Why do I get the following error, what am I missing : /
Fatal error: Call to undefined method FPDF::WriteHTML2()
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
how to save this pdf created file,
i tried with this code
file_put_contents(“/pdf/file.pdf”, $output);
i found it,
$pdf->Output(“pdf/PDF.pdf”, “F”);
how to send this pdf file after creating that
how to send Email this PDF file after creating that
how to usea whole html page with css ?
Any help here?
i have edited that demo form with Unicode language so it’s not display properly. so how we can display it is there any other way with fpdf
How to Created PDF File is Direcly Open in Default client email?(mailto:)
Is there any way where we can display line number in generated pdf?
download file link is not working
trash!!
how the save generated pdf file in a folder as well as it path in db.Please reply as sson as possible
how can i get WriteHTML.php file?
bhai adha adhura kaam kyu karte ho jo bhi kro pura karo… mention kar diya writehtml.php. kaha se laaye ye file….aur download link kha hai…..n
what is writeHTML.php ? and how i have to create it ?
Thanks for your tutorial sir. where to find WriteHtml.php.
Hi sir,Where is WriteHtml.php?
Nice Article About How to Convert HTML to Php . Its save time who need it .
very nice
what is Writehtml.php