Screenshots and PDFs
PDFs
Set format=pdf
to get a PDF instead of an image. The page is printed the way Chrome prints it: text stays selectable, links stay clickable, and the PDF is split into pages.
A web page as a PDF
// npm install @shotsmith/sdk
import { Shotsmith, TakeOptions } from '@shotsmith/sdk';
import { writeFile } from 'node:fs/promises';
const shotsmith = new Shotsmith({
accessKey: 'YOUR_ACCESS_KEY',
});
const options = TakeOptions.url('https://shotsmith.dev/sample')
.format('pdf')
.pdfPaperFormat('a4')
.pdfPrintBackground()
.pdfMargin('1cm');
await writeFile('capture.pdf', await shotsmith.take(options));
<?php
// composer require shotsmith/shotsmith-php
require 'vendor/autoload.php';
use Shotsmith\Client;
use Shotsmith\Options\TakeOptions;
$shotsmith = new Client('YOUR_ACCESS_KEY');
$options = TakeOptions::url('https://shotsmith.dev/sample')
->format('pdf')
->pdfPaperFormat('a4')
->pdfPrintBackground()
->pdfMargin('1cm');
file_put_contents('capture.pdf', $shotsmith->take($options));
// In Laravel: Shotsmith::take($options) with SHOTSMITH_ACCESS_KEY in .env
curl -G "https://shotsmith.dev/v1/take" \ --data-urlencode "access_key=YOUR_ACCESS_KEY" \ --data-urlencode "url=https://shotsmith.dev/sample" \ --data-urlencode "format=pdf" \ --data-urlencode "pdf_paper_format=a4" \ --data-urlencode "pdf_print_background=true" \ --data-urlencode "pdf_margin=1cm" \ -o capture.pdf
import { writeFile } from 'node:fs/promises';
const params = new URLSearchParams({
access_key: 'YOUR_ACCESS_KEY',
url: 'https://shotsmith.dev/sample',
format: 'pdf',
pdf_paper_format: 'a4',
pdf_print_background: 'true',
pdf_margin: '1cm',
});
const res = await fetch(`https://shotsmith.dev/v1/take?${params}`);
if (!res.ok) throw new Error((await res.json()).error_message);
await writeFile('capture.pdf', Buffer.from(await res.arrayBuffer()));
import urllib.request
from urllib.parse import urlencode
params = {
"access_key": "YOUR_ACCESS_KEY",
"url": "https://shotsmith.dev/sample",
"format": "pdf",
"pdf_paper_format": "a4",
"pdf_print_background": "true",
"pdf_margin": "1cm"
}
url = "https://shotsmith.dev/v1/take?" + urlencode(params, doseq=True)
# Raises urllib.error.HTTPError on a 4xx/5xx response
with urllib.request.urlopen(url, timeout=120) as res, open("capture.pdf", "wb") as f:
f.write(res.read())
<?php
$query = http_build_query([
'access_key' => 'YOUR_ACCESS_KEY',
'url' => 'https://shotsmith.dev/sample',
'format' => 'pdf',
'pdf_paper_format' => 'a4',
'pdf_print_background' => 'true',
'pdf_margin' => '1cm',
], '', '&', PHP_QUERY_RFC3986);
$ch = curl_init('https://shotsmith.dev/v1/take?' . $query);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 120,
]);
$body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
if ($status !== 200) {
throw new RuntimeException(json_decode($body, true)['error_message'] ?? "HTTP {$status}");
}
file_put_contents('capture.pdf', $body);
package main
import (
"io"
"net/http"
"os"
)
func main() {
res, err := http.Get("https://shotsmith.dev/v1/take?access_key=YOUR_ACCESS_KEY&url=https%3A%2F%2Fshotsmith.dev%2Fsample&format=pdf&pdf_paper_format=a4&pdf_print_background=true&pdf_margin=1cm")
if err != nil {
panic(err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
body, _ := io.ReadAll(res.Body)
panic(string(body))
}
out, err := os.Create("capture.pdf")
if err != nil {
panic(err)
}
defer out.Close()
io.Copy(out, res.Body)
}
require "net/http"
require "uri"
uri = URI("https://shotsmith.dev/v1/take")
uri.query = URI.encode_www_form([
["access_key", "YOUR_ACCESS_KEY"],
["url", "https://shotsmith.dev/sample"],
["format", "pdf"],
["pdf_paper_format", "a4"],
["pdf_print_background", "true"],
["pdf_margin", "1cm"]
])
res = Net::HTTP.get_response(uri)
raise res.body unless res.is_a?(Net::HTTPSuccess)
File.binwrite("capture.pdf", res.body)
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Path;
public class Capture {
public static void main(String[] args) throws Exception {
var client = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder(URI.create(
"https://shotsmith.dev/v1/take?access_key=YOUR_ACCESS_KEY&url=https%3A%2F%2Fshotsmith.dev%2Fsample&format=pdf&pdf_paper_format=a4&pdf_print_background=true&pdf_margin=1cm"
)).build();
var res = client.send(request, HttpResponse.BodyHandlers.ofFile(Path.of("capture.pdf")));
if (res.statusCode() != 200) {
throw new RuntimeException("HTTP " + res.statusCode());
}
}
}
using System;
using System.IO;
using System.Net.Http;
using var http = new HttpClient { Timeout = TimeSpan.FromSeconds(120) };
// Throws HttpRequestException on a 4xx/5xx response
var bytes = await http.GetByteArrayAsync(
"https://shotsmith.dev/v1/take?access_key=YOUR_ACCESS_KEY&url=https%3A%2F%2Fshotsmith.dev%2Fsample&format=pdf&pdf_paper_format=a4&pdf_print_background=true&pdf_margin=1cm"
);
await File.WriteAllBytesAsync("capture.pdf", bytes);
Content-Type: application/pdf
X-Capture-Id: 01m8kvpk6jsmeykdfs0wy6awbn
X-Cache: MISS
X-Render-Time: 264
X-Quota-Limit: 15000
X-Quota-Remaining: 14998
X-RateLimit-Limit: 80
X-RateLimit-Remaining: 79
(the PDF)
Your own HTML as a PDF
For invoices, reports and tickets, send the HTML you generate in html
. Use a POST request: HTML is too long for a URL, and it may contain customer data that shouldn’t end up in logs. Images, fonts and stylesheets can be linked by absolute URL or inlined.
import { Shotsmith } from '@shotsmith/sdk';
const shotsmith = new Shotsmith({ accessKey: 'YOUR_ACCESS_KEY' });
import { writeFile } from 'node:fs/promises';
const file = await shotsmith.take({
html: '<h1>Invoice #1042</h1><p>Total: £120.00</p>',
format: 'pdf',
pdf_paper_format: 'a4',
pdf_print_background: true,
});
await writeFile('invoice.pdf', file);
<?php
require 'vendor/autoload.php';
use Shotsmith\Client;
$shotsmith = new Client('YOUR_ACCESS_KEY');
$file = $shotsmith->take([
'html' => '<h1>Invoice #1042</h1><p>Total: £120.00</p>',
'format' => 'pdf',
'pdf_paper_format' => 'a4',
'pdf_print_background' => true,
]);
file_put_contents('invoice.pdf', $file);
curl -X POST "https://shotsmith.dev/v1/take" \
-H "X-Access-Key: YOUR_ACCESS_KEY" \
-H "Content-Type: application/json" \
-d '{
"html": "<h1>Invoice #1042</h1><p>Total: £120.00</p>",
"format": "pdf",
"pdf_paper_format": "a4",
"pdf_print_background": true
}' \
--fail-with-body \
-o invoice.pdf
import { writeFile } from 'node:fs/promises';
const res = await fetch('https://shotsmith.dev/v1/take', {
method: 'POST',
headers: {
'X-Access-Key': 'YOUR_ACCESS_KEY',
'Content-Type': 'application/json',
},
body: JSON.stringify({
html: '<h1>Invoice #1042</h1><p>Total: £120.00</p>',
format: 'pdf',
pdf_paper_format: 'a4',
pdf_print_background: true,
}),
});
if (!res.ok) throw new Error((await res.json()).error_message);
await writeFile('invoice.pdf', Buffer.from(await res.arrayBuffer()));
import json
import urllib.request
body = {
"html": "<h1>Invoice #1042</h1><p>Total: £120.00</p>",
"format": "pdf",
"pdf_paper_format": "a4",
"pdf_print_background": True
}
req = urllib.request.Request(
"https://shotsmith.dev/v1/take",
data=json.dumps(body).encode(),
headers={"X-Access-Key": "YOUR_ACCESS_KEY", "Content-Type": "application/json"},
method="POST",
)
# Raises urllib.error.HTTPError on a 4xx/5xx response
with urllib.request.urlopen(req, timeout=120) as res, open("invoice.pdf", "wb") as f:
f.write(res.read())
<?php
$ch = curl_init('https://shotsmith.dev/v1/take');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => ['X-Access-Key: YOUR_ACCESS_KEY', 'Content-Type: application/json'],
CURLOPT_POSTFIELDS => json_encode([
'html' => '<h1>Invoice #1042</h1><p>Total: £120.00</p>',
'format' => 'pdf',
'pdf_paper_format' => 'a4',
'pdf_print_background' => true,
]),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 120,
]);
$body = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_RESPONSE_CODE) >= 400) {
throw new RuntimeException(json_decode($body, true)['error_message']);
}
file_put_contents('invoice.pdf', $body);
package main
import (
"io"
"net/http"
"os"
"strings"
)
func main() {
body := strings.NewReader(`{
"html": "<h1>Invoice #1042</h1><p>Total: £120.00</p>",
"format": "pdf",
"pdf_paper_format": "a4",
"pdf_print_background": true
}`)
req, _ := http.NewRequest("POST", "https://shotsmith.dev/v1/take", body)
req.Header.Set("X-Access-Key", "YOUR_ACCESS_KEY")
req.Header.Set("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
msg, _ := io.ReadAll(res.Body)
panic(string(msg))
}
out, err := os.Create("invoice.pdf")
if err != nil {
panic(err)
}
defer out.Close()
io.Copy(out, res.Body)
}
require "net/http"
require "json"
body = {
"html" => "<h1>Invoice #1042</h1><p>Total: £120.00</p>",
"format" => "pdf",
"pdf_paper_format" => "a4",
"pdf_print_background" => true
}
res = Net::HTTP.post(
URI("https://shotsmith.dev/v1/take"),
body.to_json,
"X-Access-Key" => "YOUR_ACCESS_KEY",
"Content-Type" => "application/json"
)
raise res.body unless res.is_a?(Net::HTTPSuccess)
File.binwrite("invoice.pdf", res.body)
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Path;
public class Request {
public static void main(String[] args) throws Exception {
var body = """
{
"html": "<h1>Invoice #1042</h1><p>Total: £120.00</p>",
"format": "pdf",
"pdf_paper_format": "a4",
"pdf_print_background": true
}
""";
var request = HttpRequest.newBuilder(URI.create("https://shotsmith.dev/v1/take"))
.header("X-Access-Key", "YOUR_ACCESS_KEY")
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(body))
.build();
var res = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofFile(Path.of("invoice.pdf")));
if (res.statusCode() != 200) {
throw new RuntimeException("HTTP " + res.statusCode());
}
}
}
using System;
using System.IO;
using System.Net.Http;
using System.Text;
using var http = new HttpClient();
http.DefaultRequestHeaders.Add("X-Access-Key", "YOUR_ACCESS_KEY");
var body = """
{
"html": "<h1>Invoice #1042</h1><p>Total: £120.00</p>",
"format": "pdf",
"pdf_paper_format": "a4",
"pdf_print_background": true
}
""";
var res = await http.PostAsync(
"https://shotsmith.dev/v1/take",
new StringContent(body, Encoding.UTF8, "application/json")
);
res.EnsureSuccessStatusCode();
await File.WriteAllBytesAsync("invoice.pdf", await res.Content.ReadAsByteArrayAsync());
Content-Type: application/pdf
X-Capture-Id: 01m8kvpk6jsmeykdfs0wy6awbn
X-Cache: MISS
X-Render-Time: 163
X-Quota-Limit: 15000
X-Quota-Remaining: 14998
X-RateLimit-Limit: 80
X-RateLimit-Remaining: 79
(the PDF, written to invoice.pdf)
Page layout
| Option | Default | Does |
|---|---|---|
pdf_paper_format
| letter | a0–a6, letter, legal, tabloid or ledger |
pdf_landscape
| false | Landscape pages |
pdf_margin
| 0 | Margin on all sides with units, such as 20px or 1cm. pdf_margin_top
and the other sides override it. |
pdf_print_background
| false | Print background colours and images. Most designs need this. |
pdf_fit_one_page
| false | The whole page on a single long PDF page, with no page breaks |
Print styles
PDFs use the page’s print stylesheet, so a site that hides its navigation or changes fonts for printing looks the same here as when someone prints it. Set media_type=screen
to get the on-screen design instead.
In HTML you send, CSS controls the pages:
/* Start each section on a new page */
.section { break-before: page; }
/* Keep a table row or card in one piece */
tr, .card { break-inside: avoid; }
Everything else that works for screenshots works for PDFs too: hiding elements, blocking banners and waiting for content, async rendering and uploading to your bucket.