Multiple HTML files to a single PDF?

Most examples I’ve seen use a single HTML page. Looking at the documentation, it’s still unclear – is it possible to pull in multiple HTML pages and generate a single PDF file since my pages are separated by chapter? If it’s possible, would I use a glob statement? Do I need to install additional NPM packages?

Using the CLI:

$ pagedjs-cli /dist/**/*.html -o mybook.pdf

Hi there, that’s not possible at the moment.

you won’t be able to have a single pdf out of multiple HTML files.

Your best option is to merge them first and run paged.js after.

But it would be interresting to have something that allows it. I’ll ask fred for that.

Thanks @julien. Yes, I think I can have Nuxt handle that part first.

Hi @djmtype,

Using multiple HTML files is possible as I recall (see https://github.com/traffaillac/html-phd-manuscript). First you need to define the following function

<script>
	var req = new XMLHttpRequest();
	function include(file) {
		req.open('GET', file, false);
		req.send();
		document.write(req.responseText);
	}
</script>

Then use <script>include('file.html')</script> anywhere in the body of the main file to dump the content there. Since the browser executes the function right after parsing the closing </script>, it will put the content at the right place.

The only catch is you have to disable CORS. With Firefox this is easy as there is an option to disable it permanently. With Chromium you have to launch the browser with specific flags (https://superuser.com/questions/1217863/how-can-i-prevent-chrome-from-enforcing-cors-for-one-specific-file-url). I recall pagedjs-cli used Chromium, so you have to find the folder containing the chromium binary it is using, then run it beforehand with the right flags, then run pagedjs-cli which will reuse the Chromium browser already running (with CORS disabled).

I hope it helps,
Thibault

Oh, that’s great to know!