Solved

Creating print-friendly PDFs using PHP?

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

776 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