First, download the latest HTML2PDF library and extract it to your SilverStripe root directory.
Then add following lines in your site config file:
/* { html2pdf */
$path = Director::baseFolder().’/html2pdf/’;
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
require_once ‘html2pdf.class.php’;
/* html2pdf } */
Now you can use html2pdf library anywhere you wish .
Normal usage
$contentHtml=”<h2>Hello world </h2>”;
$pdf = new HTML2PDF(‘P’,'A4′,’en’);
$pdf->WriteHTML($contentHtml);
$pdf->Output(‘sample.pdf’);
Preparing the PDF Document
Document Information
You can specify the title, author, … of your PDF document as follows
$html2pdf = new HTML2PDF(‘P’, ‘A4′, ‘en’);
$html2pdf->pdf->SetAuthor(‘LAST-NAME Frist-Name’);
$html2pdf->pdf->SetTitle(‘HTML2PDF Wiki Example’);
$html2pdf->pdf->SetSubject(‘HTML2PDF Wiki’);
$html2pdf->pdf->SetKeywords(‘HTML2PDF, TCPDF, example, wiki’);
$html2pdf->writeHTML($content_html);
$html2pdf->Output(‘file.pdf’);
Display of Document
You can change how your PDF document will be displayed using the function of SetDisplayMode
$html2pdf->pdf->SetDisplayMode(‘fullpage’);
adding new Fonts
use following function for adding new fonts
public function addFont($family, $style=”, $file=”)
Explanation of the different margins Html2pdf
This example is little explanation with diagram of different margins
<?php
ob_start();
?>
<page backtop=”7mm” backbottom=”7mm” backleft=”10mm” backright=”10mm”>
<page_header>
Page Header
</page_header>
<page_footer>
Page Footer
</page_footer>
Page Content
</page>
<?php
$content = ob_get_clean();
$pdf = new HTML2PDF(‘P’,'A4′,’en’, false, ‘ISO-8859-15′, array(mL, mT, mR, mB));
$pdf->writeHTML($content);
$pdf->Output();
?>
You want to protect your PDF document use the function of setProtection
public function SetProtection($permissions=array(), $user_pass=”, $owner_pass=null)
ex:-
01. Only Printing Allowed – ‘my_password’ Required to open PDF-
$html2pdf->pdf->SetProtection(array(‘print’), ‘my_password’);
02. Only Printing Allowed – No Password Required to open PDF
$html2pdf->pdf->SetProtection(array(‘print’), ”)
03. Only Printing Allowed – No Password Required to open PDF – ‘admin_password’ required to unlock other features
$html2pdf->pdf->SetProtection(array(‘print’), ”, ‘admin_password’);
Output Method
$html2pdf->Output(‘my_doc.pdf’);
$html2pdf->Output(‘my_doc.pdf’, false);
$html2pdf->Output(‘my_doc.pdf’, ”);
$html2pdf->Output(‘my_doc.pdf’, ‘I’);
01. Forcing the download of PDF via web browser, with a specific name
$html2pdf->Output(‘my_doc.pdf’, ‘D’);
02. Write the contents of a PDF file on the server
$html2pdf->Output(‘directory/file_xxxx.pdf’, ‘F’);
03. Retrieve the contents of the PDF and then do whatever you want
$content_PDF = $html2pdf->Output(”, true);
$content_PDF = $html2pdf->Output(”, ‘S’);