Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

How to run HTML2PDF on a generated page


Go to End
Reply


12 Posts   4735 Views

Avatar
MartinPhone

Community Member, 57 Posts

16 September 2013 at 10:42am

I'm a little confused as to where I should call HTML2PDF from.

I followed this advice and HTML2PDF is working but I'm having trouble getting my head around how I can link to this.

I have made myself a non-page class that pulls the contents of a particular page and formats it in the way I want it (it's a big boring table of the contents of that page). This is fine but I can't work out how to combine the two, ie create a PDF of this HTML file that is generated with a whole load of children and other controls.

It is like I need to generate the HTML first, and then pass all that to my getPDFTimeTable(args) function (that just runs HTML2PDF on the content passed to it in the (args)). I'm just not sure how to do that. What am I missing?

Avatar
swaiba

Forum Moderator, 1885 Posts

16 September 2013 at 2:57pm

Having tried many libraries for this I can fully recomend http://www.tcpdf.org/
Full set of examples http://www.tcpdf.org/examples.php
No external dependencies, no need for java and converts mostly all the html fine for me without having to fiddle with it

Avatar
MartinPhone

Community Member, 57 Posts

28 October 2013 at 5:29pm

Edited: 28/10/2013 5:41pm

Thanks for the reply, sorry for the delay.

I think my problem lies elsewhere - more to do with the basic problem of how to send my HTML to the function. ie how to put what is generated by my .ss file into a variable to pass to the function...

So I have my :

<table ID="TimeTable">
<caption>$Title</caption>
<% control TimeTable %>
<tr><th colspan='3' class='TimeTableDay'>Mondays</th></tr>
<% control AllChildren %>
<% if Day == Mondays %>
<tr><td>$MenuTitle</td><td>$Time</td><td>$contactName: $contactTelephone</td></tr>
<% end_if %>
<% end_control %>
<% end_control %>
</table>

I can't put getPDFTimeTable( ) around the code above but I don't know what to do so I get all the html into a variable that I can send to the function. Am I being totally stupid?!

Avatar
Devlin

Community Member, 333 Posts

2 November 2013 at 9:14am

how to put what is generated by my .ss file into a variable

see:
http://doc.silverstripe.org/framework/en/reference/templates#calling-templates-from-php-code

// parse html
$html = $this->customise($myDataObj)->renderWith($myTmpl);

// create pdf
$pdf = new DOMPDF();
$pdf->set_base_path( BASE_PATH );
$pdf->load_html($html);
$pdf->render();
file_put_contents($folder.$filename, $pdf->output());

Avatar
EKaburu

Community Member, 6 Posts

24 February 2016 at 8:17pm

Hey Guys, Anyone has a working example of tcpdf in Silverstripe? I have been trying to integrate it into a reports generation module am building with no success.

Swaiba if you have any experience with usage of the module, please help.

Avatar
swaiba

Forum Moderator, 1885 Posts

24 February 2016 at 10:10pm

>> Anyone has a working example of tcpdf in Silverstripe?

EKaburu, Silverstripe is PHP, this is a list of examples in PHP... http://www.tcpdf.org/examples.php
What is the part you are having trouble integrating?

Avatar
EKaburu

Community Member, 6 Posts

24 February 2016 at 10:17pm


Below is my code for the class that I wish to generate the pdf from.

class RaaOrders Extends ReportsModule {   
		
		public static $allowed_actions = array (
			
			'RaaOrdersPending'
			'pdf'
		);

		public static $url_handlers = array (
			
			'pending'   => 'RaaOrdersPending',
			'mypdf'     => 'pdf'
		);

		public function RaaOrdersPending(SS_HTTPRequest $request) {
			$dummy_data = 'Dummy Data';

			return array (
				'Dummy' => $dummy_data
			);

		}


	public function pdf(){
		
		// create new PDF document
		 $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

		// set document information
		$pdf->SetCreator(PDF_CREATOR);
		$pdf->SetAuthor('Nicola Asuni');
		$pdf->SetTitle('TCPDF Example 001');
		$pdf->SetSubject('TCPDF Tutorial');
		$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

		// set default header data
		$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING, array(0,64,255), array(0,64,128));
		$pdf->setFooterData(array(0,64,0), array(0,64,128));

		// set header and footer fonts
		$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
		$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

		// set default monospaced font
		$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

		// set margins
		$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
		$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
		$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

		// set auto page breaks
		$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

		// set image scale factor
		$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

		// set some language-dependent strings (optional)
		if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
		    require_once(dirname(__FILE__).'/lang/eng.php');
		    $pdf->setLanguageArray($l);
		}

		// ---------------------------------------------------------

		// set default font subsetting mode
		$pdf->setFontSubsetting(true);

		// Set font
		// dejavusans is a UTF-8 Unicode font, if you only need to
		// print standard ASCII chars, you can use core fonts like
		// helvetica or times to reduce file size.
		$pdf->SetFont('dejavusans', '', 14, '', true);

		// Add a page
		// This method has several options, check the source code documentation for more information.
		$pdf->AddPage();

		// set text shadow effect
		$pdf->setTextShadow(array('enabled'=>true, 'depth_w'=>0.2, 'depth_h'=>0.2, 'color'=>array(196,196,196), 'opacity'=>1, 'blend_mode'=>'Normal'));

		// Print a text
		$html = '<span style="background-color:yellow;color:blue;">&nbsp;PAGE 1&nbsp;</span>
		<p stroke="0.2" fill="true" strokecolor="yellow" color="blue" style="font-family:helvetica;font-weight:bold;font-size:26pt;">You can set a full page background.</p>';
		$pdf->writeHTML($html, true, false, true, false, '');

		// Close and output PDF document
		// This method has several options, check the source code documentation for more information.
		$pdf->Output('example_001.pdf', 'I');
	}



}

Then I'm putting the link:

 <a href="raa_orders/mypdf{$PDFLink}"> GET PDF </a> 
on the template RaaOrders_RaaOrdersPending.ss

When I clickthe link, it just downloads an html file with the error: Fatal error: Class 'TCPDF' not found in C:\xampp\htdocs\kreports\reports\code\RaaOrders.php on line 275

line 275 is where I have

 $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); 
in my code

I'm thinking the autoloader is not picking up the tcpdf.php class from the module but I don't know how to solve that. Any help or a push to the right direction will be greatly appreciated. I'm quite green to Silverstripe.

Avatar
swaiba

Forum Moderator, 1885 Posts

24 February 2016 at 10:23pm

seems like you are simply missing ...

require_once(BASE_PATH.'/tcpdf/tcpdf.php');

alternatively if you use composer then it sorts this bit out for you (but that error tells me you are not currently using composer)

Go to Top