* Can it support some of the CSS-3 properties like forced pagebreaks at the beginning of each chapter?
It doesn’t use HTML2PDF. I’m using my own written snippet that uses the FPDF styling to do this. so Yes, I can make a force pagebreak. In fact, every 10 chapters it does that.
* Could you have a cover with a different PDF template appended at the front of the file?
YES! Thats the reason I use FPDI, it can import PDF files. A cover or a template makes no difference
Back cover would be no problem.
* Could you have it generate a table of contents linked to the page numbers?
Yes/No. In fact I’m not sure. I do think it’s easy enough to have auto-generated PDF ’chapters’, with their internal link system. Good point though. Looking into that.
* Could you have it do an introduction with page numbers in the format of i, ii, iii, etc. then restart the page numbers at chapter 1?
Good question, I probably can. It’s relatively easy to do dynamic formatting in the document. You can even do a manual numbering.
And finally the other question...
Have you take a look about the other CMS, like joomla, can make a pdf on real time ? Perhaps it’s a way..?
Joomla sucks for what I do. I’m serious. I’m not saying it’s a bad CMS, it’s just... not appropriate for this kind of stuff. Even Wordpress would be more suitable. But at the moment there is no faster way to export a PDF than the way I do. So, this is already as real-time as it gets, baby!
ON a more serious note, I’m trying to report my findings here as well, so people can learn from them.
My current snippet:
Please note, this is NOT completely dynamic, author etc. are hard-coded. Easily changed obviously. Also, it’s purely made for a simple page and it only works with <i> tags at the moment. But it should give you a decent step up for a lot of things.
<?php
//Parent HAS to be given, we can't do anything without a parent.
if (isset($parent)) {
// &sortBY, field to sort by, defaults to pub_date.
$sortBy = (!empty($sortBy) ) ? $sortBy : 'pub_date';
// &sortHow, sort direction, defaults to ASC(ending).
// Options are: ASC, DESC
$sortHow = (!empty($sortHow) ) ? $sortHow : 'ASC';
//Fields we use.
$fields = 'longtitle,introtext,content';
$basepath = $modx->config['base_path']."assets/snippets/makepdf/";
$classpath = $basepath.'class/';
//Use nice MODX function to get children.
$pages = $modx->getActiveChildren($parent, $sortBy, $sortHow, $fields);
require_once($classpath.'fpdi.php');
/* ------------------------------------------
Header and Footer to class
------------------------------------------- */
class PDF extends FPDI {
function Header()
{
$this->SetFont('Arial','I',11);
//Position at 1.5 cm from bottom
$this->SetY(5);
//Page number
$this->Cell(0,10,'Frozen Youth',0,1,'R');
}
function Footer()
{
$this->SetFont('Arial','I',8);
//Position at 1.5 cm from bottom
$this->SetY(-15);
//Page number
$this->Cell(0,10,'Copyright 2007-2008 - Nimja.com',0,0,'L');
//Position at 1.5 cm from bottom
$this->SetY(-15);
//Page number
$this->Cell(0,10,$this->PageNo(),0,0,'R');
}
}
/* ------------------------------------------
Start PDF and set the settings.
------------------------------------------- */
// initiate FPDI
$pdf =& new PDF();
// add a page
$pdf->AddPage();
// set the sourcefile
$pdf->setSourceFile($basepath.'Page.pdf');
$pdf->SetAuthor('Nimja');
$pdf->SetCreator('Nimja');
$pdf->SetTitle('Frozen Youth');
// import page 1
$templatePage = $pdf->importPage(1);
// use the imported page and place it
$pdf->useTemplate($templatePage);
$pdf->setTemplate($templatePage);
$pdf->SetFont('Arial');
$pdf->SetTextColor(0,0,0);
$pdf->SetLineWidth(.25);
$pdf->SetDrawColor(0,0,0);
$pdf->SetMargins(10,10);
$newpage = false;
$remove = Array("\r", '<b>', '</b>');
$clean = Array('<i>', '</i>');
/* ------------------------------------------
Start content.
------------------------------------------- */
$c = 0;
$italic = false;
foreach ($pages as $page) {
//echo $page['longtitle'].' - '.$page['introtext'].'<br />';
$chapter = $page['longtitle'];
$title = $page['introtext'];
$content = str_replace($remove, '', $page['content']);
if ($c > 9) {
$pdf->AddPage();
$c=0;
}
$pdf->SetX(10);
$pdf->SetFont('Arial','',18);
$pdf->Cell(0,7,$chapter.' - '.$title,'B',1,'L');
$pdf->Ln(3);
$content = split("\n", $content);
foreach ($content as $par) {
$line = trim($par);
if (empty($line) ) {
$pdf->Ln(3);
} else {
if (strpos($line, '<i>') > -1) $italic = true;
if ($italic) {
$pdf->SetFont('Arial','i',11);
} else {
$pdf->SetFont('Arial','',11);
}
if (strpos($line, '</i>')) $italic = false;
$line = str_replace($clean, '', $line);
$pdf->MultiCell(0,4.5,$line,0,1,'L');
}
}
$pdf->Ln(10);
$italic = false;
$c++;
}
$pdf->Output('Frozen_Youth.pdf', 'd');
} else {
//Output error message because of missing parent ID
echo 'Parent ID missing!';
}
?>
ps. I should probably add more comments, but this is very much a work-in-progress.