Forum How do I...?

Multiple headers

JonoB
At the moment, I am creating PDFs with headers as follows:

@media print {
	@page { size: A4; }

	@page {
	    @top {
			content: flow(header);
		}
	}

	#header {
		flow: static(header);
	}
}


This works perfectly.

The next step that I am trying to achieve is batch printing a whole bunch of documents to a single (multi-page) PDF. The problem is that each document may contain a different header. Is there a way that Prince can handle this?

The only other alternative that I can think of is saving each document separately to file. Once all the docs are created, I can join/merge all the PDF's programatically.
mikeday
You can wrap each sub-document in a separate named page, and apply a different header style to each one. Or you can keep the same header style, and just flow a different block up to the header in each sub-document.
JonoB
Mike,

Thanks for the response.

A bit more background. We have a few preset CSS designs that users can choose from. So, we have a static HTML page, and they can choose from any of the 5 design templates that we give them. This works fine when users are printing a page at a time - we just inject the css into the html and send it to Prince.

However, when printing multiple pages, this methodology wont work since the CSS selectors will override each other - the last template will take precedence over all others.

Other than saving each page to PDF individually and then merging them manually, the only other option that I can think of is that we customise the HTML selectors for each CSS template.

This is what we currently have (obviously very simplified):

// page.html
<div class="page-header">My Page Header</div>
    
// blue.css
.page-header {
  font-color: blue;
}
    
// green.css
.page-header {
  font-color: green;
}


I'm proposing that we change to the following:

// blue.html
<div class="blue page-header">My Page Header</div>
    
// green.html
<div class="green page-header">My Page Header</div>
    
// styles.css
.blue .page-header {
  font-color: blue;
}
    
.green .page-header {
  font-color: green;
}



Before we go down this route, I just want to make sure that we're not missing something obvious here. Are there any other options that we should be considering?
mikeday
Okay yes, that approach does make sense. Are you passing multiple HTML documents to Prince to generate a single PDF output file, or are you merging all the HTML and passing one input file to Prince?
JonoB
At the moment, we are merging all the html first and sending that one html file to Prince to generate the PDF.

I did not even think about sending multiple individual HTML files to Prince, and that may actually be a better option. We could then go back to our original solution of having a static HTML file, and just injecting the custom css for each page. Assuming that each HTML file has its own CSS file, would Prince know that each CSS file only applies to the HTML document that its linked/embedded from?
JonoB
OK, it looks like the .net and java SDK's have a ConvertMultiple() method, but I couldn't see one in the PHP implementation?
mikeday
We have just added this to the PHP wrapper, it will be available for download tomorrow. :)
mikeday
Today we have uploaded the new PHP wrapper for Prince that supports converting multiple files.
JonoB
Mike

Thanks!

I'm not sure if you know or not, but PHP is currently undergoing a mild revolution in terms of standardising formats and structure. More information is available at http://www.php-fig.org/ This is the "way forward" for PHP.

I've spent some time modifying the PHP wrapper for Prince so that it conforms with this, whilst at the same time maintaining backwards compatibility. None of the functionality has been changed; it only includes changes to function names, proper docblocks and code formatting.

Would you like a copy?
mikeday
Sure, with your permission we would be happy to integrate any changes into the wrapper, so that everyone can benefit. Just drop me an email (mikeday@yeslogic.com).