Add HTML Header Footer to PDF - Branding API
PDF4me Add HTML Header Footer to PDF enables you to add HTML content as headers, footers, or both to PDF documents. This API service processes PDF files and adds HTML content with precise positioning, custom margins, and page targeting options for document branding and organization. The API receives PDF content and HTML data through REST API calls, utilizing Base64 encoding for secure PDF transmission. With support for HTML content, flexible positioning, custom margins, page targeting, and first page exclusion, this solution is ideal for document branding, report generation, and enterprise PDF workflows.
Authenticating Your API Request
To access the PDF4me REST API, every request must include proper authentication credentials. Authentication ensures secure communication and validates your identity as an authorized user of the REST API.
Key Features
- HTML Content Support: Add rich HTML content as headers and footers with full styling support
- Flexible Positioning: Control header and footer placement with precise margin control
- Custom Styling: Full HTML and CSS support for professional appearance
- Page Targeting: Apply headers and footers to specific pages or entire documents
- Skip First Page: Option to exclude headers and footers from the first page
- Professional Results: Create headers and footers that enhance document organization and branding
- Seamless API Integration: RESTful API designed for automated PDF editing workflows and enterprise system integration
REST API Endpoint
The PDF4me REST API uses standard HTTP methods to interact with resources. All header and footer addition operations are performed through a single endpoint:
- Method: POST
- Endpoint:
/api/v2/AddHtmlHeaderFooter
Header Footer Features
HTML Content Support
- HTML Content: Support for HTML content with inline CSS styling
- Custom Styling: Use inline CSS for fonts, colors, alignment, and basic styling
- Text and Images: Support for HTML text content and embedded images
- Plain HTML String: HTML content must be provided as a plain string (not Base64 encoded)
Positioning and Layout
- Margin Control: Precise positioning with pixel-based measurements (marginLeft, marginRight, marginTop, marginBottom)
- Page Targeting: Apply to specific pages using page options (empty string for all pages, "1", "1,3,5", "2-5", "1,3,7-10", "2-")
- Skip First Page: Option to exclude headers and footers from the first page
- Location Options: Add content as Header, Footer, or Both
Advanced Features
- Asynchronous Processing: Support for async processing with polling for long-running operations
- Professional Appearance: Consistent formatting and styling across documents
- Flexible Positioning: Control header and footer placement with exact pixel measurements
REST API Parameters
Complete list of parameters for the Add HTML Header Footer to PDF REST API. Parameters are organized by category for better understanding and implementation.
Important: Parameters marked with an asterisk (*) are required and must be provided for the API to function correctly.
Required Parameters
| Parameter | Type | Description | Example |
|---|---|---|---|
| docName* | String | Output PDF file name with .pdf extension | output.pdf |
| docContent* | Base64 | The content of the input PDF file encoded in Base64 | JVBERi... |
| htmlContent* | String | HTML content (plain HTML string, NOT base64 encoded) to be added as header or footer | <div style="text-align: center; font-family: Arial; font-size: 12px; color: #FF0000;">Document Header PDF4me</div> |
| location* | String | Location for HTML content: Header, Footer, or Both | Header |
Optional Parameters
| Parameter | Type | Description | Example |
|---|---|---|---|
| pages | String | Page options: empty string for all pages, "1" for page 1, "1,3,5" for specific pages, "2-5" for page range, "1,3,7-10" for mixed, "2-" for pages from 2 to end | "" or "1-3" |
| skipFirstPage | Boolean | Skip first page (true) or include first page (false) | false |
| marginLeft | Double | Left margin in pixels | 20.0 |
| marginRight | Double | Right margin in pixels | 20.0 |
| marginTop | Double | Top margin in pixels | 50.0 |
| marginBottom | Double | Bottom margin in pixels | 50.0 |
| async | Boolean | Enable asynchronous processing. When true, the API returns a 202 status and provides a polling URL in the Location header | true |
Important Note: The
htmlContentparameter must be a plain HTML string, not Base64 encoded. You can include inline CSS styling within the HTML.
Output
The PDF4me Add HTML Header Footer to PDF REST API returns different responses based on the processing mode. The API returns the PDF as a Base64-encoded string in JSON format, not as binary data.
- Success Response
- Asynchronous Processing
- Error Responses
- Response Format Details
Synchronous Processing (Default)
When async is not set or set to false, the API processes the request immediately:
Status Code: 200 OK
Content-Type: application/json
Response Body:
{
"docName": "output.pdf",
"docContent": "JVBERi0xLjQKJeLjz9MKMSAwIG9iago8PAovVHlwZSAvQ2F0YWxvZwovUGFnZXMgMiAwIFIKPj4KZW5kb2JqCjIgMCBvYmoKPDwKL1R5cGUgL1BhZ2VzCi9LaWRzIFszIDAgUl0KL0NvdW50IDEKPD4KZW5kb2JqCjMgMCBvYmoKPDwKL1R5cGUgL1BhZ2UKL1BhcmVudCAyIDAgUgovTWVkaWFCb3ggWzAgMCA2MTIgNzkyXQovUmVzb3VyY2VzIDw8Ci9Gb250IDw8Ci9GMSA0IDAgUgo+Pgo+PgovQ29udGVudHMgNSAwIFIKPj4KZW5kb2JqCjQgMCBvYmoKPDwKL1R5cGUgL0ZvbnQKL1N1YnR5cGUgL1R5cGUxCi9CYXNlRm9udCAvSGVsdmV0aWNhCj4+CmVuZG9iago1IDAgb2JqCjw8Ci9MZW5ndGggNDQKPj4Kc3RyZWFtCkJUCi9GMSAxMiBUZgoxMDAgNzAwIFRkCihIZWxsbyBXb3JsZCkgVGoKRVQKZW5kc3RyZWFtCmVuZG9iagp4cmVmCjAgNgowMDAwMDAwMDAwIDY1NTM1IGYgCjAwMDAwMDAwMDkgMDAwMDAgbiAKMDAwMDAwMDA1NCAwMDAwMCBuIAowMDAwMDAwMTAxIDAwMDAwIG4gCjAwMDAwMDAxNzAgMDAwMDAgbiAKMDAwMDAwMDI0NCAwMDAwMCBuIAp0cmFpbGVyCjw8Ci9TaXplIDYKL1Jvb3QgMSAwIFIKPj4Kc3RhcnR4cmVmCjM0MQolJUVPRg=="
}
Response Fields:
- docName (string): The output PDF file name
- docContent (string): The PDF file with HTML header/footer applied, encoded as Base64 string
How to Use:
- Extract the
docContentfield from the JSON response - Decode the Base64 string to get the binary PDF data
- Save or process the PDF file as needed
Example (JavaScript):
const response = await fetch(url, options);
const data = await response.json();
const pdfBytes = atob(data.docContent); // Decode Base64
// Save or process pdfBytes
Asynchronous Processing
When async: true is set, the API processes the request asynchronously:
Status Code: 202 Accepted
Response Headers:
Location: https://api.pdf4me.com/api/v2/AddHtmlHeaderFooter/poll/12345-abcde-67890
Polling Process:
- Extract the
Locationheader from the 202 response - Poll the URL using GET requests
- Continue polling until you receive a 200 OK status
- The final response contains the same JSON format with
docNameanddocContent(Base64)
Polling Example:
// Initial request returns 202 with Location header
const response = await fetch(url, options);
const location = response.headers.get('Location');
// Poll until complete
while (true) {
const pollResponse = await fetch(location, { headers: { 'Authorization': 'Basic ' + apiKey } });
if (pollResponse.status === 200) {
const data = await pollResponse.json();
// Process data.docContent (Base64 PDF)
break;
}
await new Promise(resolve => setTimeout(resolve, 2000)); // Wait 2 seconds
}
Error Responses
The API returns standard HTTP error codes with error details:
- Invalid request parameters
- Missing required fields (
docName,docContent,htmlContent,location) - Invalid Base64 encoding in
docContent - Invalid location value (must be: Header, Footer, or Both)
- Invalid HTML content
- Invalid or missing API key
- API key not properly Base64 encoded in Authorization header
- Missing
Authorization: Basicheader
- Server-side processing error
- PDF processing failure
- HTML rendering failure
Error Response Format:
{
"error": "Error message describing what went wrong"
}
Response Format Details
Important: The API always returns JSON, never binary PDF data directly.
Response Structure:
{
"docName": "string", // Output PDF filename
"docContent": "string" // Base64-encoded PDF content
}
Content-Type Header:
- Success:
application/json - The PDF is embedded as a Base64 string within the JSON response
Why Base64?
- JSON-safe encoding for binary data
- Easy to transmit over HTTP
- Compatible with all programming languages
- Can be directly embedded in JSON without escaping issues
Decoding Base64 to PDF:
JavaScript/Node.js:
const base64 = response.docContent;
const binary = atob(base64); // Browser
// OR
const binary = Buffer.from(base64, 'base64').toString('binary'); // Node.js
Python:
import base64
pdf_bytes = base64.b64decode(response['docContent'])
with open('output.pdf', 'wb') as f:
f.write(pdf_bytes)
C#:
byte[] pdfBytes = Convert.FromBase64String(response.docContent);
File.WriteAllBytes("output.pdf", pdfBytes);
Request Example
Header
Content-Type: application/json
Authorization: Basic YOUR_BASE64_ENCODED_API_KEY
Note:
- Get your API key from the PDF4me Dashboard
- The API key must be Base64 encoded and prefixed with "Basic " in the Authorization header
- Example: If your API key is
abc123, encode it to Base64 and useAuthorization: Basic YWJjMTIz
Payload
Basic Example (Required Fields Only):
{
"docName": "output.pdf",
"docContent": "JVBERi0xLjQKJeLjz9MKMSAwIG9iago8PAovVHlwZSAvQ2F0YWxvZwovUGFnZXMgMiAwIFIKPj4KZW5kb2JqCjIgMCBvYmoKPDwKL1R5cGUgL1BhZ2VzCi9LaWRzIFszIDAgUl0KL0NvdW50IDEKPD4KZW5kb2JqCjMgMCBvYmoKPDwKL1R5cGUgL1BhZ2UKL1BhcmVudCAyIDAgUgovTWVkaWFCb3ggWzAgMCA2MTIgNzkyXQovUmVzb3VyY2VzIDw8Ci9Gb250IDw8Ci9GMSA0IDAgUgo+Pgo+PgovQ29udGVudHMgNSAwIFIKPj4KZW5kb2JqCjQgMCBvYmoKPDwKL1R5cGUgL0ZvbnQKL1N1YnR5cGUgL1R5cGUxCi9CYXNlRm9udCAvSGVsdmV0aWNhCj4+CmVuZG9iago1IDAgb2JqCjw8Ci9MZW5ndGggNDQKPj4Kc3RyZWFtCkJUCi9GMSAxMiBUZgoxMDAgNzAwIFRkCihIZWxsbyBXb3JsZCkgVGoKRVQKZW5kc3RyZWFtCmVuZG9iagp4cmVmCjAgNgowMDAwMDAwMDAwIDY1NTM1IGYgCjAwMDAwMDAwMDkgMDAwMDAgbiAKMDAwMDAwMDA1NCAwMDAwMCBuIAowMDAwMDAwMTAxIDAwMDAwIG4gCjAwMDAwMDAxNzAgMDAwMDAgbiAKMDAwMDAwMDI0NCAwMDAwMCBuIAp0cmFpbGVyCjw8Ci9TaXplIDYKL1Jvb3QgMSAwIFIKPj4Kc3RhcnR4cmVmCjM0MQolJUVPRg==",
"htmlContent": "<div style='text-align: center; font-family: Arial; font-size: 12px; color: #FF0000;'>Document Header PDF4me</div>",
"location": "Header"
}
Advanced Example (With All Optional Fields):
{
"docName": "output.pdf",
"docContent": "JVBERi0xLjQKJeLjz9MKMSAwIG9iago8PAovVHlwZSAvQ2F0YWxvZwovUGFnZXMgMiAwIFIKPj4KZW5kb2JqCjIgMCBvYmoKPDwKL1R5cGUgL1BhZ2VzCi9LaWRzIFszIDAgUl0KL0NvdW50IDEKPD4KZW5kb2JqCjMgMCBvYmoKPDwKL1R5cGUgL1BhZ2UKL1BhcmVudCAyIDAgUgovTWVkaWFCb3ggWzAgMCA2MTIgNzkyXQovUmVzb3VyY2VzIDw8Ci9Gb250IDw8Ci9GMSA0IDAgUgo+Pgo+PgovQ29udGVudHMgNSAwIFIKPj4KZW5kb2JqCjQgMCBvYmoKPDwKL1R5cGUgL0ZvbnQKL1N1YnR5cGUgL1R5cGUxCi9CYXNlRm9udCAvSGVsdmV0aWNhCj4+CmVuZG9iago1IDAgb2JqCjw8Ci9MZW5ndGggNDQKPj4Kc3RyZWFtCkJUCi9GMSAxMiBUZgoxMDAgNzAwIFRkCihIZWxsbyBXb3JsZCkgVGoKRVQKZW5kc3RyZWFtCmVuZG9iagp4cmVmCjAgNgowMDAwMDAwMDAwIDY1NTM1IGYgCjAwMDAwMDAwMDkgMDAwMDAgbiAKMDAwMDAwMDA1NCAwMDAwMCBuIAowMDAwMDAwMTAxIDAwMDAwIG4gCjAwMDAwMDAxNzAgMDAwMDAgbiAKMDAwMDAwMDI0NCAwMDAwMCBuIAp0cmFpbGVyCjw8Ci9TaXplIDYKL1Jvb3QgMSAwIFIKPj4Kc3RhcnR4cmVmCjM0MQolJUVPRg==",
"htmlContent": "<div style='text-align: center; font-family: Arial; font-size: 12px; color: #FF0000;'>Document Header PDF4me</div>",
"location": "Header",
"pages": "",
"skipFirstPage": false,
"marginLeft": 20.0,
"marginRight": 20.0,
"marginTop": 50.0,
"marginBottom": 50.0,
"async": true
}
Important: The
htmlContentfield must contain plain HTML as a string, not Base64 encoded. You can use inline CSS styling within the HTML tags.
Code Samples
The PDF4me Add HTML Header Footer to PDF REST API provides code samples in multiple programming languages. Choose the language that best fits your development environment:
- C#
- Java
- JavaScript
- Python
- Salesforce
- n8n
- Google Script
- AWS Lambda
Google Script Sample
Google Apps Script implementation for Google Workspace integration:
Industry Use Cases & Applications
- Legal & Professional Services
- Business & Enterprise
- Education & Research
- Finance & Banking
Legal & Professional Services Use Cases
- Legal Documentation: Apply legal disclaimers and confidentiality notices to legal documents
- Contract Headers: Add headers and footers to contracts and agreements
- Compliance Documentation: Include compliance notices in headers and footers
- Client Documentation: Add professional headers and footers to client documents
Business & Enterprise Use Cases
- Document Branding: Add company logos, headers, and footers to PDF documents
- Report Generation: Add consistent headers and footers to generated reports
- Business Documents: Enhance professional appearance with branded headers and footers
- Corporate Branding: Apply consistent headers and footers across business documents
Education & Research Use Cases
- Academic Papers: Add page numbers, headers, and footers to academic publications
- Research Documents: Format research papers with proper headers and footers
- Thesis Formatting: Add headers and footers to theses and dissertations
- Educational Materials: Enhance educational documents with headers and footers
Finance & Banking Use Cases
- Financial Reports: Add headers and footers to financial reports and statements
- Invoice Formatting: Apply headers and footers to invoices and financial documents
- Compliance Documentation: Include compliance information in headers and footers
- Audit Reports: Add professional headers and footers to audit reports