• 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
php code
<?php
include('../mpdf.php');
$mpdf=new mPDF();
$mpdf->WriteHTML('<p style="color:red;">Hallo World<br/>Fisrt sentencee</p>');
$mpdf->Output(); exit;
?>
[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..)
php code
<?php
$source="example.pdf";
$output_fold="FinalFolder";

if (!file_exists($output_fold)) { mkdir($output_fold, 0777, true);}
$result= passthru("pdftohtml $source $output_fold/new_filename",$log);
//var_dump($result); var_dump($log);
?>

  • Convert HTML to PDF in php:
php javascript
<?php
require 'pdfcrowd.php';

$client = new Pdfcrowd("username", "apikey");
$pdf = $client->convertURI('http://bbc.co.uk/');
?>

  • converts a web page and sends the generated PDF as an HTTP
php javascript
<?php
require 'pdfcrowd.php';
try
{
// create an API client instance
$client = new Pdfcrowd("username", "apikey");
// convert a web page and store the generated PDF into a $pdf variable
$pdf = $client->convertURI('http://www.google.com/');
// set HTTP response headers
header("Content-Type: application/pdf");
header("Cache-Control: max-age=0");
header("Accept-Ranges: none");
header("Content-Disposition: attachment; filename=\"google_com.pdf\"");
// send the generated PDF
echo $pdf;
}
catch(PdfcrowdException $why)
{
echo "Pdfcrowd Error: " . $why;
}
?>
[ad type=”banner”]

  • using HTML 2 FPDF
php in html
<?php
require('html2fpdf.php');
$pdf=new HTML2FPDF();

$pdf->AddPage();
$fp = fopen("scriptarticle-sample.html","r");
$strContent = fread($fp, filesize("scriptarticle-sample.html"));
fclose($fp);

$pdf->WriteHTML($strContent);
$pdf->Output("sample.pdf");
echo "PDF file has been generated successfully!";

?>

  • PDF from HTML file
php in html
<?php
$html = file_get_contents("pdf-content.html");
$dompdf->loadHtml($html);

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF (1 = download and 0 = preview)
$dompdf->stream("codexworld",array("Attachment"=>0));
?>

  • TCPDF:
php in html
<?php
require_once('tcpdf_include.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 049');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 049', PDF_HEADER_STRING);
]$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
$pdf->SetFont('helvetica', '', 10);
php website code
$pdf->AddPage();
$html = '<h1>Test TCPDF Methods in HTML</h1>
<h2 style="color:red;">IMPORTANT:</h2>
<span style="color:red;">If we’re using user-generated content, the tcpdf tag can be unsafe.<br />
We can disable this tag by setting to false the <b>K_TCPDF_CALLS_IN_HTML</b> constant on TCPDF configuration file.</span>
<h2>write1DBarcode method in HTML</h2>';
$params = TCPDF_STATIC::serializeTCPDFtagParameters(array('CODE 39', 'C39', '', '', 80, 30, 0.4, array('position'=>'S', 'border'=>true, 'padding'=>4, 'fgcolor'=>array(0,0,0), 'bgcolor'=>array(255,255,255), 'text'=>true, 'font'=>'helvetica', 'fontsize'=>8, 'stretchtext'=>4), 'N'));
$html .= '<tcpdf method="write1DBarcode" params="'.$params.'" />';
$params = TCPDF_STATIC::serializeTCPDFtagParameters(array('CODE 128', 'C128', '', '', 80, 30, 0.4, array('position'=>'S', 'border'=>true, 'padding'=>4, 'fgcolor'=>array(0,0,0), 'bgcolor'=>array(255,255,255), 'text'=>true, 'font'=>'helvetica', 'fontsize'=>8, 'stretchtext'=>4), 'N'));
$html .= '<tcpdf method="write1DBarcode" params="'.$params.'" />';
$html .= '<tcpdf method="AddPage" /><h2>Graphic Functions</h2>';
$params = TCPDF_STATIC::serializeTCPDFtagParameters(array(0));
$html .= '<tcpdf method="SetDrawColor" params="'.$params.'" />';
$params = TCPDF_STATIC::serializeTCPDFtagParameters(array(50, 50, 40, 10, 'DF', array(), array(0,128,255)));
$html .= '<tcpdf method="Rect" params="'.$params.'" />';
$pdf->writeHTML($html, true, 0, true, 0);
$pdf->lastPage();
$pdf->Output('example_049.pdf', 'I');
?>
[ad type=”banner”]

  • Using DOMPDF generate a PDF.
php website code
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . "/path/to/dompdf");

require_once "dompdf_config.inc.php";

$dompdf = new DOMPDF();

$html = <<<'ENDHTML'
<html>
<body>
<h1>Hello Dompdf</h1>
</body>
</html>
ENDHTML;

$dompdf->load_html($html);
$dompdf->render();

$dompdf->stream("hello.pdf");

  • To generate pdf

 

php web development code
<?php
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();

$output = $dompdf->output();
file_put_contents("/path/to/file.pdf", $output);
[ad type=”banner”]

  • Using PHP
    php web development code
    <?php
    $dompdf = new DOMPDF();
    $dompdf->set_paper("A4");
    // load the html content
    $dompdf->load_html($html);
    $dompdf->render();
    $canvas = $dompdf->get_canvas();
    $font = Font_Metrics::get_font("helvetica", "bold");
    $canvas->page_text(16, 800, "Page: {PAGE_NUM} of {PAGE_COUNT}", $font, 8, array(0,0,0));
    $dompdf->stream("sample.pdf",array("Attachment"=>0));
  • 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: