Solved

Creating print-friendly PDFs using PHP?

Posted on 2014-03-18
1
624 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 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
In this tutorial viewers will learn how to style different bullet points for unordered lists in CSS. Begin with a normal unordered list; the default bullet point is a solid circle: In the CSS, create a defined class of unordered list by typing "ul.S…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now