PDF documents such as ebooks, statements, brochures, and reports often require headers, footers, and page numbers. More complex documents may need title page layouts or headers to vary from page to page.
Fortunately, it’s easy to make these complex documents with DocRaptor’s HTML to PDF API and their PHP library. You may be familiar with some of the open-source HTML to PDF libraries, but they are almost all based on browser engines and cannot create the complex PDFs described above or even some of the basic PDF pages without hacks and polyfills.
This tutorial will walk you through DocRaptor’s simple setup and show some basic PDF conversion examples.
First, add docraptor to your Gemfile or require the docraptor gem.
Then, instantiate the agent and authenticate with your DocRaptor API key. Alternatively, you can use their YOUR_API_KEY_HERE trial key which doesn’t even require creating an account (it only works for test mode documents though):
$docraptor = new DocRaptor\DocApi();
Then it’s as simple as:
$doc = new DocRaptor\Doc();
// $prince_options = new DocRaptor\PrinceOptions();
$create_response = $docraptor->createDoc($doc);
The PHP library is pretty straightforward, but what I really appreciate about DocRaptor is how easy the HTML and CSS is. For example, the headers and footer code is just:
<a href=”https://docraptor.com”>DocRaptor is awesome!</a>
<img src=”https://docraptor.com/docraptor-logo.png” />
<!– Note: The header content is ABOVE the rest of the document content –>
<p>Normal document content</p>
And page numbers work like:
content: “Page ” counter(page) ” of ” counter(pages);
That page number content can also be inserted into the header or footer.
There are a lot more examples and links to other agents on the DocRaptor documentation site. Give them a look!