Solved

Creating print-friendly PDFs using PHP?

Posted on 2014-03-18
1
629 Views
Last Modified: 2014-03-24
I recently created a short PHP script that reads an XLS file, loops through all of it's rows, and then outputs it's contents to a browser:

http://egoselfaxis.net/index.php

The page I'm generating is very fluid at the moment, and is not especially print friendly (when I print using the "CutePDF Writer" print driver .. I get only 2 products per page).

How should I re-format my HTML and CSS (or PHP code) so that I end up with nicely formatted multi-page PDFs that have at least 8-12 items per page (assuming that all of the items/boxes are the exact same size)?  

My PHP code is below:

<?php
	error_reporting(E_ALL ^ E_NOTICE);
	require_once 'excel/excel_reader2.php';
	$data = new Spreadsheet_Excel_Reader("colors.xls");
	$numrows = $data->rowcount($sheet_index=0);
	$startrow = '92';
	for ( $counter = $startrow; $counter <= $numrows ; $counter++) {
		$productid = $data->val($counter,2);	
		if ($productid != '') {	
			echo '<h1 style="clear:left;width:99%;padding:5px;background-color:#FACA43;">' . $productid . "</h1>";	
		}	
		$imgurl = $data->val($counter,16);	
		if ($imgurl != '') {
			echo '<div style="float:left;padding:10px;border:1px solid #333;margin:0 15px 15px 0;text-align:center;width:250px;height:500px;">';		
			echo '<img src="http://www.tasseldepot.com/' . $imgurl . '" /><br />';
			echo '<strong>' . $data->val($counter,12) . '</strong><br /><br />';		
			echo '</div>';	
		}
	}
?>

Open in new window


Thanks,
- Yvan
0
Comment
Question by:egoselfaxis
1 Comment
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39936860
HTML is a semantic markup language; PDF is a page-layout language.  These are not quite as different as fish and bicycles, but they have less overlap than you might think.  I've found good results generating PDF documents with FPDF and TCPDF.  They are a little fiddly at first and require some time to set up the displays, but once you get them working they are consistent and lightning fast.

I have not tested either of them past PHP5.3+ but that aside, I think you would get better results with the PDF if you put the data explicitly on the page, exactly where you want it, in exactly the size you choose.

If you can give us some test data, and a link to the Excel Reader that you're using we might be able to show you a sample script.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question