CREATING PDF'S FROM VERY LARGE PAGES - Jan 6th, 2015


As the cookbook grew to well over 400 recipes (records) in late 2014, conversion to PDF became a problem. Server memory
allocation issues were keeping the PDFs from being created.

Dave Edis from Interactive Tools came up with a simple, elegant solution for creating multiple PDFs, each containing a
set number of recipes (records) , and displaying a paging menu at the top of the viewer.

At the top of the viewer, the load records call contains a conditional statement that determines when perPage
limitations are implemented. In this case when there is a request for a PDF to be created. (The 'loadCreatedBy' =>
false, reduces the server load slightly). There’s also a createPDF_fromOutput instruction.


if (@$_REQUEST['pdf']) {
list($table_of_contentsRecords, $table_of_contentsMetaData) = getRecords(array(
'tableName' => 'table_of_contents',
'perPage' => 100,
'loadCreatedBy' => false,
));
}
else {
list($table_of_contentsRecords, $table_of_contentsMetaData) = getRecords(array(
'tableName' => 'table_of_contents',
'loadCreatedBy' => false,
));
}

if (@$_REQUEST['pdf']) { createPDF_fromOutput('inline', 'example.pdf'); }


Then in the body, this code changes the background color of the page if a PDF is requested:


<?php if (@$_REQUEST['pdf']): ?>
<body style="background-color:#FFF">
<?php else: ?>
<body style="background-color:#9FF">
<?php endif ?>


And the code that creates a dynamic paging menu:


<?php
$recordCount = mysql_count('table_of_contents');
$recordsPerPage = 100;
$pageCount = 1;

print
"We now have $recordCount recipes!\n";

while ((
$pageCount * $recordsPerPage) < ($recordCount + $recordsPerPage)) {
$recipesLow = sprintf("%03s", ($pageCount-1) * $recordsPerPage + 1 );
$recipesHigh = min($recordCount, ($pageCount * $recordsPerPage) );

print
"Section $pageCount (Recipes $recipesLow - $recipesHigh)";
print
" - <a href='cookbook.php?page=$pageCount'>View HTML</a>";
print
" - <a href='cookbook.php?page=$pageCount&amp;pdf=1'>Download PDF</a>";

$pageCount = $pageCount + 1; // show next page
}
?>



The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service