Forum How do I...?

Print multiple using PHP

JonoB
Recently 2 new methods were added to the php class: convert_multiple_files() and convert_multiple_files_to_passthru()

Is it possible to also convert multiple strings? It would be great if we could have convert_multiple_strings_to_passthru() and convert_multiple_strings_to_file(). Is this even doable?
mikeday
No, currently this is not possible, as only a single document can be written to Prince through standard input. Does each string contain an entirely separate HTML document, or would it be possible for you to append them together to make a single document?
JonoB
What I am doing at the moment to get around this is writing the individual documents to file, and then passing all the individual files as an array to convert_multiple_files(). Obviously there is some overhead in writing all the files to disk.

I can indeed append all the strings together. However, do I then need to make a single HTML document (i.e. only 1 head and body), or can it be multiple individual html documents (i.e. each with their own head and body)?

I'm guessing the former, but just wanted to double check.
mikeday
Combining it into a single valid document would be ideal if you are able to do that. Just appending them all together may work, but could cause issues due to repeated elements like <head>, <title>, <style>, etc.

Are you creating the documents in your own code, or retrieving them from elsewhere?
JonoB
Hey Mike,

We create all these docs from scratch ourselves.

A quick breakdown: users can create css "themes" for individual documents. They can have as many themes as they wish, each with custom css. Each document is then allocated a theme by the user. For each user, it may be that all documents have the exact same theme, or they may each have a completely different theme or (most likely) somewhere in between. Its totally up to each user which themes they do or don't want to use.

We've tried two approaches:

i) One long html document (with page breaks where appropriate) with all the css (for all themes) injected into the header. The downside to this approach is that each individual page needs to be namespaced ("template-blue", "template-green", "template-custom", etc) as does the css.

ii) Create multiple individual html files, each with only the relevant css injected into the header. This gets around the namespacing issue completely, as each document is treated as standalone. We then feed all the individual documents to Prince, which merges them into one pdf.

The second option gets around a lot of added complexity in terms of namespacing, etc, but has the one downside of the extra overhead of writing the html files to disk (and then deleting them after the pdf has been created). Our testing indicates that its not too significant at the moment, but we will continue to monitor.

To be fair, in the majority of cases, users only print one document at a time, in which case we just feed the string directly to Prince. Its only in cases where multiple documents are printed that we write them out to disk.

It would be awesome if we could feed an array of strings to Prince, and Prince handles them as it would for an array of html documents.
mikeday
I'll add this to the roadmap, but we'll need to come up with a sensible way of encoding multiple documents for the Prince input stream, so it's not something we can do in a hurry.
mikeday
Prince now has a control protocol which allows this!