Customizing PDF Output: Complete Guide to Options
Master all PDF customization options available in HTMLtoPDF.ac API. Learn about page sizes, margins, headers, footers, and more.
Customizing PDF Output: Complete Guide to Options
The HTMLtoPDF.ac API offers extensive customization options to create PDFs that match your exact requirements. This guide covers all available options.
Page Format and Size
Choose from standard page sizes or specify custom dimensions:
{
"options": {
"format": "A4" // Options: A4, Letter, Legal, Tabloid, Ledger, A0-A6
}
}Or use custom dimensions:
{
"options": {
"width": "8.5in",
"height": "11in"
}
}Orientation
Control page orientation:
{
"options": {
"landscape": true // false for portrait (default)
}
}Margins
Set custom margins for all sides:
{
"options": {
"margin": {
"top": "20mm",
"right": "20mm",
"bottom": "20mm",
"left": "20mm"
}
}
}You can use different units: mm, cm, in, px.
Headers and Footers
Add custom headers and footers using HTML templates:
{
"options": {
"displayHeaderFooter": true,
"headerTemplate": "<div style='font-size: 10px; text-align: center; width: 100%;'>Header Content</div>",
"footerTemplate": "<div style='font-size: 10px; text-align: center; width: 100%;'>Page <span class='pageNumber'></span> of <span class='totalPages'></span></div>"
}
}Special classes available:
pageNumber: Current page numbertotalPages: Total number of pagesdate: Current datetitle: Document titleurl: Document URL
Background Graphics
Include background colors and images:
{
"options": {
"printBackground": true
}
}Scale
Control the scale of the page:
{
"options": {
"scale": 1.0 // 0.1 to 2.0
}
}Page Ranges
Generate only specific pages:
{
"options": {
"pageRanges": "1-5,8,11-13"
}
}Complete Example
Here's a complete example with multiple options:
{
"html": "<html><body><h1>My Document</h1></body></html>",
"options": {
"format": "A4",
"landscape": false,
"margin": {
"top": "20mm",
"right": "15mm",
"bottom": "20mm",
"left": "15mm"
},
"displayHeaderFooter": true,
"headerTemplate": "<div style='text-align: center; font-size: 10px;'>My Company</div>",
"footerTemplate": "<div style='text-align: center; font-size: 10px;'>Page <span class='pageNumber'></span></div>",
"printBackground": true,
"scale": 1.0,
"preferCSSPageSize": false
}
}CSS Page Size
Use CSS @page rules for page size:
{
"options": {
"preferCSSPageSize": true
}
}Then in your HTML:
@page {
size: A4 landscape;
margin: 20mm;
}Tips
- Test different margin values to find what works best
- Use headers/footers for branding and page numbers
- Enable
printBackgroundfor colored backgrounds - Adjust scale for fine-tuning output size
- Use CSS @page for complex page layouts