{"id":447,"date":"2024-02-19T05:02:28","date_gmt":"2024-02-19T05:02:28","guid":{"rendered":"http:\/\/www.phpgang.com\/?p=447---24bfbaa7-df88-4b33-bddd-4be4ffad6fc4"},"modified":"2024-02-19T05:02:28","modified_gmt":"2024-02-19T05:02:28","slug":"how-to-convert-html-to-pdf-in-php-with-fpdf","status":"publish","type":"post","link":"https:\/\/www.phpgang.com\/how-to-convert-html-to-pdf-in-php-with-fpdf_447.html","title":{"rendered":"How to Convert HTML to PDF in PHP with fpdf"},"content":{"rendered":"

HTML<\/a> 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<\/em>\u00a0open source and very useful library for developers here is a simple tutorial on how to convert\u00a0How to Convert HTML to PDF with fpdf.<\/p>\n

\"convert-html-to-pdf\"<\/p>\n

[wpdm_file id=58]DEMO<\/a><\/div>\n

You have to download fpdf<\/a> library and \u00a0include it in your PHP<\/a> 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<\/a>\u00a0you can download and use as per your requirement.<\/p>\n

index.html<\/strong><\/p>\n

In this file I have created a simple contact form<\/a> data on submit it show that submitted data on PDF format:<\/p>\n

<html>\r\n<head>\r\n<link href=\"css\/bootstrap.min.css\" rel=\"stylesheet\" media=\"screen\">\r\n<link href=\"css\/bootstrap-responsive.min.css\" rel=\"stylesheet\" media=\"screen\">\r\n<script type=\"text\/javascript\" src=\"js\/jquery-1.8.0.min.js\"><\/script>\r\n<script type=\"text\/javascript\" src=\"js\/bootstrap.min.js\"><\/script>\r\n<title>How to create Contact Form using Bootstrap  | PGPGang.com<\/title>\r\n<\/head>\r\n\r\n<body>\r\n<h2>How to create Contact Form using Bootstrap example.&nbsp;&nbsp;&nbsp;=> <a href=\"https:\/\/www.phpgang.com\/\">Home<\/a> | <a href=\"http:\/\/demo.phpgang.com\/\">More Demos<\/a><\/h2>\r\n<div class=\"container\">\r\n      <form class=\"contact-us form-horizontal\" action=\"actionpdf.php\" method=\"post\">\r\n        <legend>Fill Form and submit to generate PDF<\/legend>        \r\n        <div class=\"control-group\">\r\n            <label class=\"control-label\">Name<\/label>\r\n            <div class=\"controls\">\r\n                <div class=\"input-prepend\">\r\n                <span class=\"add-on\"><i class=\"icon-user\"><\/i><\/span>\r\n                    <input type=\"text\" class=\"input-xlarge\" name=\"name\" placeholder=\"Name\">\r\n                <\/div>\r\n            <\/div>\r\n        <\/div>\r\n        <div class=\"control-group\">\r\n            <label class=\"control-label\">Email<\/label>\r\n            <div class=\"controls\">\r\n                <div class=\"input-prepend\">\r\n                <span class=\"add-on\"><i class=\"icon-envelope\"><\/i><\/span>\r\n                    <input type=\"text\" class=\"input-xlarge\" name=\"email\" placeholder=\"Email\">\r\n                <\/div>\r\n            <\/div>    \r\n        <\/div>\r\n        <div class=\"control-group\">\r\n            <label class=\"control-label\">Url<\/label>\r\n            <div class=\"controls\">\r\n                <div class=\"input-prepend\">\r\n                <span class=\"add-on\"><i class=\"icon-globe\"><\/i><\/span>\r\n                    <input type=\"text\" id=\"url\" class=\"input-xlarge\" name=\"url\" placeholder=\"http:\/\/www.example.com\">\r\n                <\/div>\r\n            <\/div>\r\n        <\/div>\r\n        <div class=\"control-group\">\r\n            <label class=\"control-label\">Comment<\/label>\r\n            <div class=\"controls\">\r\n                <div class=\"input-prepend\">\r\n                <span class=\"add-on\"><i class=\"icon-pencil\"><\/i><\/span>\r\n                    <textarea name=\"comment\" class=\"span4\" rows=\"4\" cols=\"80\" placeholder=\"Comment (Max 200 characters)\"><\/textarea>\r\n                <\/div>\r\n            <\/div>\r\n        <\/div>\r\n        <div class=\"control-group\">\r\n          <div class=\"controls\">\r\n            <button type=\"submit\" class=\"btn btn-primary\">Submit<\/button>\r\n            <button type=\"button\" class=\"btn\">Cancel<\/button>\r\n          <\/div>    \r\n        <\/div>\r\n      <\/form>\r\n<\/div>\r\n<\/body>\r\n<\/html><\/pre>\n

actionpdf.php<\/strong><\/p>\n

This file contain PHP code to generate pdf file and show your submitted data on that file.<\/p>\n

<?php\r\nrequire('WriteHTML.php');\r\n\r\n$pdf=new PDF_HTML();\r\n\r\n$pdf->AliasNbPages();\r\n$pdf->SetAutoPageBreak(true, 15);\r\n\r\n$pdf->AddPage();\r\n$pdf->Image('logo.png',18,13,33);\r\n$pdf->SetFont('Arial','B',14);\r\n$pdf->WriteHTML('<para><h1>PHPGang Programming Blog, Tutorials, jQuery, Ajax, PHP, MySQL and Demos<\/h1><br>\r\nWebsite: <u>www.phpgang.com<\/u><\/para><br><br>How to Convert HTML to PDF with fpdf example');\r\n\r\n$pdf->SetFont('Arial','B',7); \r\n$htmlTable='<TABLE>\r\n<TR>\r\n<TD>Name:<\/TD>\r\n<TD>'.$_POST['name'].'<\/TD>\r\n<\/TR>\r\n<TR>\r\n<TD>Email:<\/TD>\r\n<TD>'.$_POST['email'].'<\/TD>\r\n<\/TR>\r\n<TR>\r\n<TD>URl:<\/TD>\r\n<TD>'.$_POST['url'].'<\/TD>\r\n<\/TR>\r\n<TR>\r\n<TD>Comment:<\/TD>\r\n<TD>'.$_POST['comment'].'<\/TD>\r\n<\/TR>\r\n<\/TABLE>';\r\n$pdf->WriteHTML2(\"<br><br><br>$htmlTable\");\r\n$pdf->SetFont('Arial','B',6);\r\n$pdf->Output(); \r\n?><\/pre>\n

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.<\/p>\n

$pdf->Image('logo.png',18,13,33);\r\n$pdf->SetFont('Arial','B',14);<\/pre>\n

These lines used to add a logo and select font size for heading.<\/p>\n

$pdf->SetFont('Arial','B',7);<\/pre>\n

Select small font then heading for inner content.<\/p>\n

$pdf->WriteHTML2(\"<br><br><br>$htmlTable\");\r\n$pdf->Output();<\/pre>\n

Write HTML to pdf file and output that file on the web browser.<\/p>\n

Support<\/strong><\/p>\n

If you need any help regarding its configuration please feel free to comment we love to help you.<\/p>\n

Facebook<\/a><\/blockquote><\/div><\/div>
Tutorial Categories:<\/strong>
\n \"HTML\"<\/a><\/div>
\n \"PHP\"<\/a><\/div><\/div>","protected":false},"excerpt":{"rendered":"

HTML<\/a> 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<\/em>\u00a0open source and very useful library for developers here is a simple tutorial on how to convert\u00a0How to Convert HTML to PDF with fpdf.<\/p>\n

\"convert-html-to-pdf\"<\/p>\n","protected":false},"author":1,"featured_media":448,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"twitterCardType":"","cardImageID":0,"cardImage":"","cardTitle":"","cardDesc":"","cardImageAlt":"","cardPlayer":"","cardPlayerWidth":0,"cardPlayerHeight":0,"cardPlayerStream":"","cardPlayerCodec":"","footnotes":""},"categories":[164,11,3],"tags":[166,269,270,623,22,23,25,21,271,20,24],"_links":{"self":[{"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/posts\/447"}],"collection":[{"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/comments?post=447"}],"version-history":[{"count":0,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/posts\/447\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/media\/448"}],"wp:attachment":[{"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/media?parent=447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/categories?post=447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/tags?post=447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}