This is a demonstration of how to use Eleventy and Prince to create a website and a PDF document from the same content.
This project was created by Larry Hudson. You can find the source code on GitHub.
If you're sharing information online, the best format for the end user is a webpage:
This website aims to demonstrate these benefits:
If your content is long, or designed to be read in a specific order, then a 'print PDF' option can be useful for users.
Some users may prefer to print out the document and read it, especially if they are not comfortable reading on a screen.
If your content has many different sections, then a well-formatted PDF that has everything the user needs in one place, can be a great experience for the user.
A big part of accessibility is making sure people can access the information in their preferred way. So offering different formats is good for accessibility!
If you are publishing a document in multiple formats (eg. a print document designed in InDesign, a Microsoft Word version and HTML content to be inserted into a CMS), it can be difficult to keep each version up to date. If a content change needs to be made, you might need to change it in three places.
By generating multiple formats from the same source of truth, we can be sure that any content changes automatically come through to the generated output.
This project uses two tools:
Here is a summary of the workflow:
Eleventy makes it possible to pull in content from many different sources. So you can write your content in the way you prefer.
You can use:
Inside your Eleventy project, you create the HTML template that the content will be inserted into.
You create two separate templates:
In this project, the /pdf-content/ page is the one that gets converted to a PDF.
You can browse the source code of this project to see an example of this.
When you run the 'build' command, Eleventy will build the HTML pages ready for publishing.
With the eleventy-plugin-prince-pdf plugin installed, Prince will automatically convert the specified webpage to a PDF after Eleventy builds the website.
If you deploy to a platform like Netlify, it will automatically rebuild the website each time you make changes to the Git repository. If you're using a CMS, you can also set up a 'webhook' that tells Netlify to rebuild the site, when you make content changes.
As you can see in the PDF, Prince does a lot more than your browser's 'print to PDF' function.
It makes it possible to do things like:
Prince does this with custom CSS rules.
This code, in src/css/print.css
, controls the header and footer content:
/* Set variables to use in the header and footer */
#doctitle {
string-set: doctitle content();
}
h2.page-break-before {
string-set: sectionName content();
}
/* Add page number to bottom of pager */
@page {
/* Add document title to top left */
@top-left {
content: string(doctitle);
}
/* Add section name to bottom left */
@bottom-left {
content: string(sectionName);
}
/* Add page number to bottom right */
@bottom-right {
content: "Page " counter(page) " of " counter(pages);
}
}
You can find out more in the Paged Media section of Prince's User Guide.
This project is open source, available on GitHub.
Feel free to download the code, have a play and send some feedback. You can reach me on Twitter.