• we have an HTML (not XHTML) document that renders fine in Firefox 3 and IE 7. It uses fairly basic CSS to style it and renders fine in HTML.
  • ways of converting it to PDF.
  • DOMPDF: it had huge problems with tables. we figured out that our large nested tables and it helped (before it was just consuming up to 128M of memory then dying—that’s the limit of memory in php.ini) but it works completely in tables and not in images. The tables were just basic stuff with some border styles to add some lines at various points;
  • HTML2PDF and HTML2PS: It rendered some of the images (all the images are Google Chart URLs) and the table formatting was much better but it seemed to have some complexity and it keeps dying with unknown node_type() errors.
  • Htmldoc: It works fine on basic HTML but has almost no support for CSS whatsoever so we have to do everything in HTML so it’s useless to us.
  • we tried a Windows app called Html2Pdf Pilot and it works perfect but we need something which slightly runs on Linux and ideally runs on-demand via PHP on the Webserver.

  • use MPDF
  • a) extract in our folder
  • b) create file.php in our folder and insert such code:
  • c) open file.php from our browser
[pastacode lang=”php” manual=”%3C%3Fphp%0Ainclude(‘..%2Fmpdf.php’)%3B%0A%24mpdf%3Dnew%20mPDF()%3B%0A%24mpdf-%3EWriteHTML(‘%3Cp%20style%3D%22color%3Ared%3B%22%3EHallo%20World%3Cbr%2F%3EFisrt%20sentencee%3C%2Fp%3E’)%3B%0A%24mpdf-%3EOutput()%3B%20%20%20exit%3B%0A%20%3F%3E%0A” message=”php code” highlight=”” provider=”manual”/] [ad type=”banner”]

Use pdf to HTML:

  • extract pdftohtml.exe to our root folder
  • inside that folder, in anyfile.php file, Insert this code (assuming, there is a source example.pdf too)
  • enter FinalFolder, and there will be the converted files (as many pages, as the source PDF had..)
[pastacode lang=”php” manual=”%3C%3Fphp%0A%24source%3D%22example.pdf%22%3B%0A%24output_fold%3D%22FinalFolder%22%3B%0A%0A%20%20%20%20if%20(!file_exists(%24output_fold))%20%7B%20mkdir(%24output_fold%2C%200777%2C%20true)%3B%7D%0A%24result%3D%20passthru(%22pdftohtml%20%24source%20%24output_fold%2Fnew_filename%22%2C%24log)%3B%0A%2F%2Fvar_dump(%24result)%3B%20var_dump(%24log)%3B%0A%3F%3E%0A” message=”php code” highlight=”” provider=”manual”/]

  • Convert HTML to PDF in php:
[pastacode lang=”php” manual=”%3C%3Fphp%0Arequire%20’pdfcrowd.php’%3B%0A%0A%24client%20%3D%20new%20Pdfcrowd(%22username%22%2C%20%22apikey%22)%3B%0A%24pdf%20%3D%20%24client-%3EconvertURI(‘http%3A%2F%2Fbbc.co.uk%2F’)%3B%0A%3F%3E%0A” message=”php javascript” highlight=”” provider=”manual”/]

  • converts a web page and sends the generated PDF as an HTTP
[pastacode lang=”php” manual=”%3C%3Fphp%0Arequire%20’pdfcrowd.php’%3B%0Atry%0A%7B%20%20%20%0A%20%20%20%20%2F%2F%20create%20an%20API%20client%20instance%0A%20%20%20%20%24client%20%3D%20new%20Pdfcrowd(%22username%22%2C%20%22apikey%22)%3B%0A%20%20%20%20%2F%2F%20convert%20a%20web%20page%20and%20store%20the%20generated%20PDF%20into%20a%20%24pdf%20variable%0A%20%20%20%20%24pdf%20%3D%20%24client-%3EconvertURI(‘http%3A%2F%2Fwww.google.com%2F’)%3B%0A%20%20%20%20%2F%2F%20set%20HTTP%20response%20headers%0A%20%20%20%20header(%22Content-Type%3A%20application%2Fpdf%22)%3B%0A%20%20%20%20header(%22Cache-Control%3A%20max-age%3D0%22)%3B%0A%20%20%20%20header(%22Accept-Ranges%3A%20none%22)%3B%0A%20%20%20%20header(%22Content-Disposition%3A%20attachment%3B%20filename%3D%5C%22google_com.pdf%5C%22%22)%3B%0A%20%20%20%20%2F%2F%20send%20the%20generated%20PDF%20%0A%20%20%20%20echo%20%24pdf%3B%0A%7D%0Acatch(PdfcrowdException%20%24why)%0A%7B%0A%20%20%20%20echo%20%22Pdfcrowd%20Error%3A%20%22%20.%20%24why%3B%0A%7D%0A%3F%3E%0A” message=”php javascript” highlight=”” provider=”manual”/] [ad type=”banner”]

  • using HTML 2 FPDF
[pastacode lang=”php” manual=”%3C%3Fphp%0Arequire(‘html2fpdf.php’)%3B%0A%24pdf%3Dnew%20HTML2FPDF()%3B%0A%20%0A%24pdf-%3EAddPage()%3B%0A%24fp%20%3D%20fopen(%22scriptarticle-sample.html%22%2C%22r%22)%3B%0A%24strContent%20%3D%20fread(%24fp%2C%20filesize(%22scriptarticle-sample.html%22))%3B%0Afclose(%24fp)%3B%0A%20%0A%24pdf-%3EWriteHTML(%24strContent)%3B%0A%24pdf-%3EOutput(%22sample.pdf%22)%3B%0Aecho%20%22PDF%20file%20has%20been%20generated%20successfully!%22%3B%0A%20%0A%3F%3E%0A” message=”php in html” highlight=”” provider=”manual”/]

  • PDF from HTML file
[pastacode lang=”php” manual=”%3C%3Fphp%0A%24html%20%3D%20file_get_contents(%22pdf-content.html%22)%3B%0A%24dompdf-%3EloadHtml(%24html)%3B%0A%0A%2F%2F%20(Optional)%20Setup%20the%20paper%20size%20and%20orientation%0A%24dompdf-%3EsetPaper(‘A4’%2C%20’landscape’)%3B%0A%0A%2F%2F%20Render%20the%20HTML%20as%20PDF%0A%24dompdf-%3Erender()%3B%0A%0A%2F%2F%20Output%20the%20generated%20PDF%20(1%20%3D%20download%20and%200%20%3D%20preview)%0A%24dompdf-%3Estream(%22codexworld%22%2Carray(%22Attachment%22%3D%3E0))%3B%0A%3F%3E%0A” message=”php in html” highlight=”” provider=”manual”/]

  • TCPDF:
[pastacode lang=”php” manual=”%3C%3Fphp%0Arequire_once(‘tcpdf_include.php’)%3B%0A%24pdf%20%3D%20new%20TCPDF(PDF_PAGE_ORIENTATION%2C%20PDF_UNIT%2C%20PDF_PAGE_FORMAT%2C%20true%2C%20’UTF-8’%2C%20false)%3B%0Apdf-%3ESetCreator(PDF_CREATOR)%3B%0A%24pdf-%3ESetAuthor(‘Nicola%20Asuni’)%3B%0A%24pdf-%3ESetTitle(‘TCPDF%20Example%20049’)%3B%0A%24pdf-%3ESetSubject(‘TCPDF%20Tutorial’)%3B%0A%24pdf-%3ESetKeywords(‘TCPDF%2C%20PDF%2C%20example%2C%20test%2C%20guide’)%3B%0A%24pdf-%3ESetHeaderData(PDF_HEADER_LOGO%2C%20PDF_HEADER_LOGO_WIDTH%2C%20PDF_HEADER_TITLE.’%20049’%2C%20PDF_HEADER_STRING)%3B%0A%5D%24pdf-%3EsetHeaderFont(Array(PDF_FONT_NAME_MAIN%2C%20”%2C%20PDF_FONT_SIZE_MAIN))%3B%0A%24pdf-%3EsetFooterFont(Array(PDF_FONT_NAME_DATA%2C%20”%2C%20PDF_FONT_SIZE_DATA))%3B%0A%24pdf-%3ESetDefaultMonospacedFont(PDF_FONT_MONOSPACED)%3B%0A%24pdf-%3ESetMargins(PDF_MARGIN_LEFT%2C%20PDF_MARGIN_TOP%2C%20PDF_MARGIN_RIGHT)%3B%0A%24pdf-%3ESetHeaderMargin(PDF_MARGIN_HEADER)%3B%0A%24pdf-%3ESetFooterMargin(PDF_MARGIN_FOOTER)%3B%0A%24pdf-%3ESetAutoPageBreak(TRUE%2C%20PDF_MARGIN_BOTTOM)%3B%0A%24pdf-%3EsetImageScale(PDF_IMAGE_SCALE_RATIO)%3B%0Aif%20(%40file_exists(dirname(__FILE__).’%2Flang%2Feng.php’))%20%7B%0A%20%20%20%20require_once(dirname(__FILE__).’%2Flang%2Feng.php’)%3B%0A%20%20%20%20%24pdf-%3EsetLanguageArray(%24l)%3B%0A%7D%0A%24pdf-%3ESetFont(‘helvetica’%2C%20”%2C%2010)%3B%0A” message=”php in html” highlight=”” provider=”manual”/] [pastacode lang=”php” manual=”%24pdf-%3EAddPage()%3B%0A%24html%20%3D%20’%3Ch1%3ETest%20TCPDF%20Methods%20in%20HTML%3C%2Fh1%3E%0A%3Ch2%20style%3D%22color%3Ared%3B%22%3EIMPORTANT%3A%3C%2Fh2%3E%0A%3Cspan%20style%3D%22color%3Ared%3B%22%3EIf%20we%E2%80%99re%20using%20user-generated%20content%2C%20the%20tcpdf%20tag%20can%20be%20unsafe.%3Cbr%20%2F%3E%0AWe%20can%20disable%20this%20tag%20by%20setting%20to%20false%20the%20%3Cb%3EK_TCPDF_CALLS_IN_HTML%3C%2Fb%3E%20constant%20on%20TCPDF%20configuration%20file.%3C%2Fspan%3E%0A%3Ch2%3Ewrite1DBarcode%20method%20in%20HTML%3C%2Fh2%3E’%3B%0A%24params%20%3D%20TCPDF_STATIC%3A%3AserializeTCPDFtagParameters(array(‘CODE%2039’%2C%20’C39’%2C%20”%2C%20”%2C%2080%2C%2030%2C%200.4%2C%20array(‘position’%3D%3E’S’%2C%20’border’%3D%3Etrue%2C%20’padding’%3D%3E4%2C%20’fgcolor’%3D%3Earray(0%2C0%2C0)%2C%20’bgcolor’%3D%3Earray(255%2C255%2C255)%2C%20’text’%3D%3Etrue%2C%20’font’%3D%3E’helvetica’%2C%20’fontsize’%3D%3E8%2C%20’stretchtext’%3D%3E4)%2C%20’N’))%3B%0A%24html%20.%3D%20’%3Ctcpdf%20method%3D%22write1DBarcode%22%20params%3D%22′.%24params.’%22%20%2F%3E’%3B%0A%24params%20%3D%20TCPDF_STATIC%3A%3AserializeTCPDFtagParameters(array(‘CODE%20128’%2C%20’C128’%2C%20”%2C%20”%2C%2080%2C%2030%2C%200.4%2C%20array(‘position’%3D%3E’S’%2C%20’border’%3D%3Etrue%2C%20’padding’%3D%3E4%2C%20’fgcolor’%3D%3Earray(0%2C0%2C0)%2C%20’bgcolor’%3D%3Earray(255%2C255%2C255)%2C%20’text’%3D%3Etrue%2C%20’font’%3D%3E’helvetica’%2C%20’fontsize’%3D%3E8%2C%20’stretchtext’%3D%3E4)%2C%20’N’))%3B%0A%24html%20.%3D%20’%3Ctcpdf%20method%3D%22write1DBarcode%22%20params%3D%22′.%24params.’%22%20%2F%3E’%3B%0A%24html%20.%3D%20’%3Ctcpdf%20method%3D%22AddPage%22%20%2F%3E%3Ch2%3EGraphic%20Functions%3C%2Fh2%3E’%3B%0A%24params%20%3D%20TCPDF_STATIC%3A%3AserializeTCPDFtagParameters(array(0))%3B%0A%24html%20.%3D%20’%3Ctcpdf%20method%3D%22SetDrawColor%22%20params%3D%22′.%24params.’%22%20%2F%3E’%3B%0A%24params%20%3D%20TCPDF_STATIC%3A%3AserializeTCPDFtagParameters(array(50%2C%2050%2C%2040%2C%2010%2C%20’DF’%2C%20array()%2C%20array(0%2C128%2C255)))%3B%0A%24html%20.%3D%20’%3Ctcpdf%20method%3D%22Rect%22%20params%3D%22′.%24params.’%22%20%2F%3E’%3B%0A%24pdf-%3EwriteHTML(%24html%2C%20true%2C%200%2C%20true%2C%200)%3B%0A%24pdf-%3ElastPage()%3B%0A%24pdf-%3EOutput(‘example_049.pdf’%2C%20’I’)%3B%0A%3F%3E%0A” message=”php website code” highlight=”” provider=”manual”/] [ad type=”banner”]

  • Using DOMPDF generate a PDF.
[pastacode lang=”php” manual=”%3C%3Fphp%0Aset_include_path(get_include_path()%20.%20PATH_SEPARATOR%20.%20%22%2Fpath%2Fto%2Fdompdf%22)%3B%0A%0Arequire_once%20%22dompdf_config.inc.php%22%3B%0A%0A%24dompdf%20%3D%20new%20DOMPDF()%3B%0A%0A%24html%20%3D%20%3C%3C%3C’ENDHTML’%0A%3Chtml%3E%0A%20%3Cbody%3E%0A%20%20%3Ch1%3EHello%20Dompdf%3C%2Fh1%3E%0A%20%3C%2Fbody%3E%0A%3C%2Fhtml%3E%0AENDHTML%3B%0A%0A%24dompdf-%3Eload_html(%24html)%3B%0A%24dompdf-%3Erender()%3B%0A%0A%24dompdf-%3Estream(%22hello.pdf%22)%3B%0A” message=”php website code” highlight=”” provider=”manual”/]

  • To generate pdf

 

[pastacode lang=”php” manual=”%3C%3Fphp%0A%24dompdf%20%3D%20new%20DOMPDF()%3B%0A%24dompdf-%3Eload_html(%24html)%3B%0A%24dompdf-%3Erender()%3B%0A%0A%24output%20%3D%20%24dompdf-%3Eoutput()%3B%0Afile_put_contents(%22%2Fpath%2Fto%2Ffile.pdf%22%2C%20%24output)%3B%0A” message=”php web development code” highlight=”” provider=”manual”/] [ad type=”banner”]

  • Using PHP[pastacode lang=”php” manual=”%3C%3Fphp%0A%24dompdf%20%3D%20new%20DOMPDF()%3B%0A%24dompdf-%3Eset_paper(%22A4%22)%3B%0A%2F%2F%20load%20the%20html%20content%0A%24dompdf-%3Eload_html(%24html)%3B%0A%24dompdf-%3Erender()%3B%0A%24canvas%20%3D%20%24dompdf-%3Eget_canvas()%3B%0A%24font%20%3D%20Font_Metrics%3A%3Aget_font(%22helvetica%22%2C%20%22bold%22)%3B%0A%24canvas-%3Epage_text(16%2C%20800%2C%20%22Page%3A%20%7BPAGE_NUM%7D%20of%20%7BPAGE_COUNT%7D%22%2C%20%24font%2C%208%2C%20array(0%2C0%2C0))%3B%0A%24dompdf-%3Estream(%22sample.pdf%22%2Carray(%22Attachment%22%3D%3E0))%3B%0A” message=”php web development code” highlight=”” provider=”manual”/]
  • This article provides some of the basic informations on css , php html , javascript html , html php , php web design , php javascript , html css javascript , php in html , javascript online editor , js online , php website , html style , javascript online , css editor online , javascript in php , html & css , online css editor , css website , html in php , css style , html to php , code editor online , css code , html script , css javascript , javascript editor online , online html editor with css , online javascript editor , javascript examples with code web design , html css editor , how to create a website using html , html and css editor , ajax data , php web development , how to make a website using html , css and html.

Categorized in: