[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Creating print-friendly PDFs using PHP?

Posted on 2014-03-18
1
Medium Priority
?
643 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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

873 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