===============
LIBRARY RULES
===============
From library maintainers:
- Send the HTML as a JSON string field named html; pdfOptions is optional
- Use preview: true while developing; it is not billed and adds a watermark
- Use uploadUrl (a presigned PUT URL) for large PDFs instead of reading the response body
- Assets such as CSS, images, and fonts are uploaded per project and referenced by filename
### External Image Reference
Source: https://github.com/heap-jp/pdfg-docs/blob/main/docs/en/image.md
HTML example showing how to reference an image hosted on the internet using a full URL.
```html
```
--------------------------------
### cURL Request for PDF Generation
Source: https://github.com/heap-jp/pdfg-docs/blob/main/docs/en/api.md
A cURL request example demonstrating how to specify HTML content and various PDF formatting options, saving the output to sample.pdf.
```bash
curl --location 'https://api.pdfg.net/v1' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer YOUR_API_KEY' \
--data '{
"html": "Hello World
",
"pdfOptions": {
"displayHeaderFooter": true,
"footerTemplate": " /
",
"format": "A4",
"headerTemplate": "Sample Header
",
"height": "297mm",
"landscape": false,
"margin": {
"top": "20mm",
"right": "20mm",
"bottom": "20mm",
"left": "20mm"
},
"omitBackground": false,
"pageRanges": "1-5",
"preferCSSPageSize": false,
"printBackground": true,
"scale": 1,
"width": "210mm"
}
}' \
--output sample.pdf
```
--------------------------------
### Upload from Dashboard: Basic Image Reference
Source: https://github.com/heap-jp/pdfg-docs/blob/main/docs/en/image.md
HTML example showing how to reference an uploaded image file (sample.png) in your HTML. Suitable for fixed images like logos. Once uploaded, images can be reused in different PDF generations.
```html
```
--------------------------------
### Generate landscape PDF with landscape property
Source: https://github.com/heap-jp/pdfg-docs/blob/main/docs/en/page.md
Switches the PDF to landscape orientation by setting 'landscape' to true in pdfOptions. The example uses A5 format.
```json
{
"pdfOptions": {
"format": "A5",
"landscape": true /* Set to landscape */
},
"html": '
Page
'
}
```
--------------------------------
### Define Jinja2 HTML template for invoice
Source: https://github.com/heap-jp/pdfg-docs/blob/main/docs/en/jinja2.md
HTML template with Jinja2 syntax for looping over items and displaying a total. Used as the template file 'templates/invoice.html' in the Python example.
```html
{# templates/invoice.html #}
Invoice
| Item |
Amount |
{% for item in items %}
| {{ item.name }} |
{{ item.price }} USD |
{% endfor %}
Total: {{ total }} USD
```
--------------------------------
### Generate A5 PDF with format property
Source: https://github.com/heap-jp/pdfg-docs/blob/main/docs/en/page.md
Sets the PDF format to A5 (148mm × 210mm) using the 'format' property. The font size is the same as the A3 example, but appears relatively larger due to the smaller page size.
```json
{
"pdfOptions": {
"format": "A5" /* Specify A5 size */
},
"html": '
Page
'
}
```
--------------------------------
### Base64 Encoded Image
Source: https://github.com/heap-jp/pdfg-docs/blob/main/docs/en/image.md
HTML example showing how to embed a Base64 encoded image directly in the HTML using a data URI. Suitable for dynamically generated images like QR codes or displaying different images per user.
```html
```
--------------------------------
### Automatic page breaks with long text
Source: https://github.com/heap-jp/pdfg-docs/blob/main/docs/en/page.md
When content exceeds the specified page size, it automatically breaks into multiple pages. This example uses A5 format and long text content.
```json
{
"pdfOptions": {
"format": "A5"
},
"html": '
Lorem ipsum dolor sit amet... /* Long text content */
'
}
```
--------------------------------
### Custom page breaks with CSS page sizes
Source: https://github.com/heap-jp/pdfg-docs/blob/main/docs/en/page.md
Creates page breaks at specific positions by specifying page sizes in CSS. This example uses two div elements set to A4 size with different background colors, and requires 'printBackground' to be true to show the backgrounds.
```json
{
"pdfOptions": {
"format": "A4", /* Specify A4 size */
"printBackground": true /* Required to show background colors */
},
"html": '
Page 1
Page 2
'
}
```
--------------------------------
### Prevent page breaks with explicit width and height
Source: https://github.com/heap-jp/pdfg-docs/blob/main/docs/en/page.md
Prevents page breaks by explicitly specifying the page size. This example uses A5 width (148mm) and double its height (420mm) to generate a single long PDF.
```json
{
"pdfOptions": {
"width": "148mm", /* A5 width */
"height": "420mm" /* Double A5 height */
},
"html": '
Lorem ipsum dolor sit amet... /* Long text content */
'
}
```
--------------------------------
### Custom font with @font-face in HTML
Source: https://github.com/heap-jp/pdfg-docs/blob/main/docs/en/font.md
HTML example showing how to specify a custom uploaded font (e.g., Armenian) using @font-face inside a style tag. The font-family name must match the uploaded font, and the src URL points to the uploaded font file.
```html
Բարև աշխարհ
```
--------------------------------
### Generate PDF from Go template using pdfg API
Source: https://github.com/heap-jp/pdfg-docs/blob/main/docs/en/go.md
The HTML template defines an invoice layout with Go template actions ({{range .Items}}, {{.Name}}, {{.Price}}, {{.Total}}) to render dynamic data. The Go program parses this template, executes it with invoice data, and sends the resulting HTML to pdfg's API endpoint https://api.pdfg.net/v1 with an Authorization header using YOUR_API_KEY. On a 200 response, it saves the returned PDF to invoice.pdf.
```html
Invoice
| Item |
Amount |
{{range .Items}}
| {{.Name}} |
{{.Price}} USD |
{{end}}
Total: {{.Total}} USD
```
```go
// main.go
package main
import (
"bytes"
"encoding/json"
"html/template"
"io"
"net/http"
"os"
)
type Item struct {
Name string
Price int
}
type Invoice struct {
Items []Item
Total int
}
type PdfgRequest struct {
HTML string `json:"html"`
PdfOptions map[string]interface{} `json:"pdfOptions"`
}
func main() {
// Load template
tmpl, err := template.ParseFiles("templates/invoice.html")
if err != nil {
panic(err)
}
// Prepare data
invoice := Invoice{
Items: []Item{
{Name: "Product A", Price: 1000},
{Name: "Product B", Price: 2000},
},
Total: 3000,
}
// Generate HTML string
var buf bytes.Buffer
if err := tmpl.Execute(&buf, invoice); err != nil {
panic(err)
}
// Create request body
reqBody := PdfgRequest{
HTML: buf.String(),
PdfOptions: map[string]interface{}{
"format": "A4",
},
}
jsonData, err := json.Marshal(reqBody)
if err != nil {
panic(err)
}
// Request to pdfg's API
req, err := http.NewRequest("POST", "https://api.pdfg.net/v1", bytes.NewBuffer(jsonData))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer YOUR_API_KEY")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// Save PDF
if resp.StatusCode == 200 {
out, err := os.Create("invoice.pdf")
if err != nil {
panic(err)
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
panic(err)
}
}
}
```
--------------------------------
### Upload from Dashboard: Path Notations
Source: https://github.com/heap-jp/pdfg-docs/blob/main/docs/en/image.md
HTML comments and img tags showing that subdirectory paths are not supported; all these notations refer to the same sample.png file.
```html
```
--------------------------------
### curl request with preview: true
Source: https://github.com/heap-jp/pdfg-docs/blob/main/docs/en/preview.md
Use this curl command to generate a PDF in preview mode. The 'preview: true' parameter displays the PDF in the dashboard preview screen and does not count towards usage quota. The generated PDF will include a 'pdfg.net preview' watermark.
```bash
curl -X POST 'https://api.pdfg.net/v1' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer YOUR_API_KEY' \
--data '{"html": "Hello World
", "preview": true}'
```
--------------------------------
### Generate PDF from ERB template using pdfg API (generate_pdf.rb)
Source: https://github.com/heap-jp/pdfg-docs/blob/main/docs/en/erb.md
Ruby script that loads the ERB template, prepares data, creates a binding object, generates the HTML string, and sends it to pdfg's API. It uses HTTParty to POST to https://api.pdfg.net/v1 with an Authorization header and saves the response as invoice.pdf if successful.
```ruby
# generate_pdf.rb
require 'erb'
require 'httparty'
# Load ERB template
template = ERB.new(File.read('templates/invoice.html.erb'))
# Prepare data
items = [
{ name: '商品A', price: 1000 },
{ name: '商品B', price: 2000 }
]
total = 3000
# Create a binding object
class TemplateBinding
def initialize(items, total)
@items = items
@total = total
end
def items
@items
end
def total
@total
end
def get_binding
binding
end
end
# Generate HTML string
html = template.result(TemplateBinding.new(items, total).get_binding)
# Request to pdfg's API
response = HTTParty.post(
'https://api.pdfg.net/v1',
headers: {
'Content-Type' => 'application/json',
'Authorization' => 'Bearer YOUR_API_KEY'
},
body: {
html: html,
pdfOptions: {
format: 'A4'
}
}.to_json
)
# PDFの保存
if response.success?
File.binwrite('invoice.pdf', response.body)
end
```
--------------------------------
### Render Jinja2 template and send HTML to pdfg API
Source: https://github.com/heap-jp/pdfg-docs/blob/main/docs/en/jinja2.md
Python script that loads the Jinja2 template, renders it with sample data, and posts the HTML to pdfg's API with an A4 format option. Saves the response content as 'invoice.pdf' on success.
```python
# generate_pdf.py
from jinja2 import Environment, FileSystemLoader
import requests
# Configure Jinja2 environment
env = Environment(loader=FileSystemLoader('templates'))
template = env.get_template('invoice.html')
# Prepare data
items = [
{'name': '商品A', 'price': 1000},
{'name': '商品B', 'price': 2000},
]
total = 3000
# Generate HTML string
html = template.render(items=items, total=total)
# Request to pdfg's API
response = requests.post(
'https://api.pdfg.net/v1',
headers={
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
},
json={
'html': html,
'pdfOptions': {
'format': 'A4'
}
}
)
# Save PDF
if response.status_code == 200:
with open('invoice.pdf', 'wb') as f:
f.write(response.content)
```
--------------------------------
### Generate PDF from React component using ReactDOMServer and pdfg API
Source: https://github.com/heap-jp/pdfg-docs/blob/main/docs/en/react.md
Defines an Invoice React component that renders an HTML table with items and total, then uses ReactDOMServer.renderToString() to convert it to an HTML string, sends it to pdfg's API with a Bearer token, and saves the returned PDF buffer to invoice.pdf.
```tsx
// Invoice.tsx
import React from "react";
interface Props {
items: { name: string; price: number }[];
total: number;
}
export const Invoice: React.FC = ({ items, total }) => (
Invoice
| Item |
Amount |
{items.map((item, i) => (
| {item.name} |
{item.price} USD |
))}
Total: {total} USD
);
```
```ts
// generatePdf.ts
import ReactDOMServer from "react-dom/server";
import { Invoice } from "./Invoice";
import fs from "fs";
// Prepare data
const items = [
{ name: "商品A", price: 1000 },
{ name: "商品B", price: 2000 },
];
// Convert JSX to HTML string
const html = ReactDOMServer.renderToString(
);
// Request to pdfg's API
const response = await fetch("https://api.pdfg.net/v1", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer YOUR_API_KEY",
},
body: JSON.stringify({
html: html,
pdfOptions: {
format: "A4",
},
}),
});
// Save PDF
if (response.ok) {
const pdfBuffer = await response.arrayBuffer();
fs.writeFileSync("invoice.pdf", Buffer.from(pdfBuffer));
}
```
--------------------------------
### Generate PDF using Blade and pdfg API in generate_pdf.php
Source: https://github.com/heap-jp/pdfg-docs/blob/main/docs/en/blade.md
Configures the Blade engine, renders the template with data, and sends the resulting HTML to the pdfg API via cURL to save as a PDF.
```php
register('blade', function () use ($compiler) {
return new CompilerEngine($compiler);
});
$finder = new FileViewFinder($files, ['templates']);
$factory = new Factory($resolver, $finder, new Illuminate\Events\Dispatcher);
// Prepare data
$items = [
['name' => '商品A', 'price' => 1000],
['name' => '商品B', 'price' => 2000],
];
$total = 3000;
// Generate HTML string
$html = $factory->make('invoice', [
'items' => $items,
'total' => $total,
])->render();
// Request to pdfg's API
$ch = curl_init('https://api.pdfg.net/v1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer YOUR_API_KEY'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
'html' => $html,
'pdfOptions' => [
'format' => 'A4'
]
]));
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// Save PDF
if ($httpCode === 200) {
file_put_contents('invoice.pdf', $response);
}
```
--------------------------------
### Generate PDF from Vue.js SSR HTML with pdfg API
Source: https://github.com/heap-jp/pdfg-docs/blob/main/docs/en/vue.md
Complete workflow: define an Invoice Vue component with props for items and total, then use createSSRApp and renderToString to generate an HTML string, send it to pdfg's API with a Bearer token, and save the returned PDF as invoice.pdf. Requires @vue/server-renderer and a valid API key.
```ts
Invoice
| Item |
Amount |
| {{ item.name }} |
{{ item.price }} USD |
Total: {{ total }} USD
```
```ts
// generatePdf.ts
import { createSSRApp } from "vue";
import { renderToString } from "@vue/server-renderer";
import Invoice from "./Invoice.vue";
import fs from "fs";
// Prepare data
const items = [
{ name: "Product A", price: 1000 },
{ name: "Product B", price: 2000 },
];
// Create Vue application
const app = createSSRApp(Invoice, {
items,
total: 3000,
});
// Convert to HTML string
const html = await renderToString(app);
// Request to pdfg's API
const response = await fetch("https://api.pdfg.net/v1", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer YOUR_API_KEY",
},
body: JSON.stringify({
html: html,
pdfOptions: {
format: "A4",
},
}),
});
// Save PDF
if (response.ok) {
const pdfBuffer = await response.arrayBuffer();
fs.writeFileSync("invoice.pdf", Buffer.from(pdfBuffer));
}
```
--------------------------------
### Apply styles using the style tag
Source: https://github.com/heap-jp/pdfg-docs/blob/main/docs/en/style.md
Define CSS rules within a
Hello World