Einschränkungen zu einem PDF hinzufügen
Legen Sie die Berechtigungs-Flags eines PDFs fest und steuern Sie damit Kopieren, Drucken, Bearbeiten und das Ausfüllen von Formularen, hinter einem Besitzerpasswort.
Wenden Sie Berechtigungseinschränkungen auf ein PDF an, um Kopieren, Drucken,
Bearbeiten, das Ausfüllen von Formularen und mehr zu begrenzen, durchgesetzt
von einem owner_password. Einschränkungen sind Berechtigungs-Flags, im
Unterschied zum Hinzufügen eines Passworts,
das nur das Passwort festlegt, das zum Öffnen der Datei nötig ist; Sie können
hier zusätzlich ein user_password setzen, um beides zu verlangen. Die API ist
stateless: Ihr Dokument wird in der Region verarbeitet und niemals
gespeichert.
Endpoint
/v1/add_restrictionsIn allen Regionen verfügbar. Hinweise zu Routing und Datenresidenz finden Sie unter Regionen und Datenresidenz.
| Region | URL |
|---|---|
| Global | https://api.pdfblocks.com/v1/add_restrictions |
| USA | https://us.api.pdfblocks.com/v1/add_restrictions |
| HIPAA USA | https://hipaa.api.pdfblocks.com/v1/add_restrictions |
| Europäische Union | https://eu.api.pdfblocks.com/v1/add_restrictions |
| Vereinigtes Königreich | https://uk.api.pdfblocks.com/v1/add_restrictions |
| Kanada | https://ca.api.pdfblocks.com/v1/add_restrictions |
| Australien | https://au.api.pdfblocks.com/v1/add_restrictions |
| Japan | https://jp.api.pdfblocks.com/v1/add_restrictions |
| Indien | https://in.api.pdfblocks.com/v1/add_restrictions |
| Brasilien | https://br.api.pdfblocks.com/v1/add_restrictions |
Authentifizierung
Authentifizieren Sie jede Anfrage mit Ihrem geheimen API-Schlüssel im Header
X-API-Key, über HTTPS. Schlüssel erstellen und verwalten Sie im
Dashboard. Einzelheiten finden Sie unter
Authentifizierung.
Anfrage
Der Endpoint nimmt einen Anfragetext vom Typ multipart/form-data entgegen.
filefilerequiredDas PDF-Eingabedokument.
owner_passwordstringrequiredDas Besitzerpasswort. 4 bis 32 druckbare ASCII-Zeichen. Es öffnet das Dokument und ändert dessen Berechtigungs-Flags.
user_passwordstringEin optionales Benutzerpasswort. 4 bis 32 druckbare ASCII-Zeichen. Ist es gesetzt, lässt sich das Dokument ohne dieses Passwort nicht öffnen. Lassen Sie es weg oder leer, damit jeder das Dokument öffnen kann.
encryption_algorithmstringdefault:AES-128Der Verschlüsselungsalgorithmus. Entweder AES-128 oder AES-256.
allow_copy_contentbooleandefault:trueErlaubt das Kopieren von Text und Bildern in die Zwischenablage.
allow_change_contentbooleandefault:trueErlaubt das Ändern des Dokumentinhalts.
allow_printbooleandefault:trueErlaubt das Drucken des Dokuments.
allow_print_high_resolutionbooleandefault:trueErlaubt das Drucken in hoher Auflösung.
allow_comment_and_fill_formbooleandefault:trueErlaubt das Hinzufügen, Bearbeiten und Ändern von Anmerkungen sowie das Ausfüllen von Formularfeldern.
allow_fill_formbooleandefault:trueErlaubt das Ausfüllen von Formularfeldern.
allow_assemble_documentbooleandefault:trueErlaubt das Zusammenstellen oder Bearbeiten des Dokuments (Seiten einfügen, löschen, drehen).
allow_accessibilitybooleandefault:trueErlaubt es Software für Barrierefreiheit, den Text und die Bilder des Dokuments zu lesen.
Berechtigungs-Flags
Jedes Flag regelt eine Aktion, die ein Leser ausführen kann. Übergeben Sie das
Flag als booleschen Wert; setzen Sie es auf false, um diese Berechtigung
abzuschalten.
| Flag | Standard | Auf false setzen, um… |
|---|---|---|
allow_copy_content |
true |
das Kopieren von Text und Bildern in die Zwischenablage zu blockieren |
allow_change_content |
true |
das Ändern des Dokumentinhalts zu blockieren |
allow_print |
true |
das Drucken des Dokuments zu blockieren |
allow_print_high_resolution |
true |
das Drucken in hoher Auflösung zu blockieren |
allow_comment_and_fill_form |
true |
das Hinzufügen, Bearbeiten und Ändern von Anmerkungen sowie das Ausfüllen von Formularfeldern zu blockieren |
allow_fill_form |
true |
das Ausfüllen von Formularfeldern zu blockieren |
allow_assemble_document |
true |
das Zusammenstellen oder Bearbeiten des Dokuments (Seiten einfügen, löschen, drehen) zu blockieren |
allow_accessibility |
true |
Software für Barrierefreiheit am Lesen des Dokuments zu hindern |
Jedes Flag steht standardmäßig auf true, es ist also nichts eingeschränkt,
solange Sie es nicht verlangen. Senden Sie nur die Flags, die Sie abschalten
wollen. Durchgesetzt werden die Einschränkungen vom owner_password: Ein
Leser, der es angibt, kann die Berechtigungen ändern. Fügen Sie nur dann ein
user_password hinzu, wenn Sie zusätzlich ein Passwort allein zum Öffnen der
Datei verlangen wollen. Zum vollständigen Lebenszyklus siehe Dokumente
schützen.
Beispiele
Sperren Sie ein Dokument so, dass es weder kopiert noch gedruckt werden kann, es aber jeder öffnen kann:
curl https://api.pdfblocks.com/v1/add_restrictions \
-H 'X-API-Key: your_api_key' \
-F file=@input.pdf \
-F owner_password='s3cr3t-owner' \
-F allow_copy_content=false \
-F allow_print=false \
-F allow_print_high_resolution=false \
-o restricted.pdf# pip install requests
import requests
with open('input.pdf', 'rb') as file:
response = requests.post(
'https://api.pdfblocks.com/v1/add_restrictions',
headers={'X-API-Key': 'your_api_key'},
files={'file': file},
data={
'owner_password': 's3cr3t-owner',
'allow_copy_content': 'false',
'allow_print': 'false',
'allow_print_high_resolution': 'false',
},
)
response.raise_for_status()
with open('restricted.pdf', 'wb') as output:
output.write(response.content)// Node.js 18+
import { readFile, writeFile } from 'node:fs/promises';
const body = new FormData();
body.set('file', new Blob([await readFile('input.pdf')]), 'input.pdf');
body.set('owner_password', 's3cr3t-owner');
body.set('allow_copy_content', 'false');
body.set('allow_print', 'false');
body.set('allow_print_high_resolution', 'false');
const response = await fetch('https://api.pdfblocks.com/v1/add_restrictions', {
method: 'POST',
headers: { 'X-API-Key': 'your_api_key' },
body,
});
if (!response.ok) throw new Error(`Request failed: ${response.status}`);
await writeFile('restricted.pdf', Buffer.from(await response.arrayBuffer()));<?php
$ch = curl_init('https://api.pdfblocks.com/v1/add_restrictions');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['X-API-Key: your_api_key'],
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => [
'file' => new CURLFile('input.pdf', 'application/pdf'),
'owner_password' => 's3cr3t-owner',
'allow_copy_content' => 'false',
'allow_print' => 'false',
'allow_print_high_resolution' => 'false',
],
]);
$pdf = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200) {
file_put_contents('restricted.pdf', $pdf);
}# gem install http
require 'http'
response = HTTP
.headers('X-API-Key' => 'your_api_key')
.post('https://api.pdfblocks.com/v1/add_restrictions', form: {
file: HTTP::FormData::File.new('input.pdf'),
owner_password: 's3cr3t-owner',
allow_copy_content: 'false',
allow_print: 'false',
allow_print_high_resolution: 'false',
})
File.write('restricted.pdf', response.body) if response.status.success?package main
import (
"bytes"
"io"
"mime/multipart"
"net/http"
"os"
)
func main() {
var buf bytes.Buffer
form := multipart.NewWriter(&buf)
file, _ := os.Open("input.pdf")
defer file.Close()
part, _ := form.CreateFormFile("file", "input.pdf")
io.Copy(part, file)
form.WriteField("owner_password", "s3cr3t-owner")
form.WriteField("allow_copy_content", "false")
form.WriteField("allow_print", "false")
form.WriteField("allow_print_high_resolution", "false")
form.Close()
req, _ := http.NewRequest("POST", "https://api.pdfblocks.com/v1/add_restrictions", &buf)
req.Header.Set("Content-Type", form.FormDataContentType())
req.Header.Set("X-API-Key", "your_api_key")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
out, _ := os.Create("restricted.pdf")
defer out.Close()
io.Copy(out, res.Body)
}using var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-API-Key", "your_api_key");
using var form = new MultipartFormDataContent
{
{ new ByteArrayContent(File.ReadAllBytes("input.pdf")), "file", "input.pdf" },
{ new StringContent("s3cr3t-owner"), "owner_password" },
{ new StringContent("false"), "allow_copy_content" },
{ new StringContent("false"), "allow_print" },
{ new StringContent("false"), "allow_print_high_resolution" },
};
var response = await client.PostAsync(
"https://api.pdfblocks.com/v1/add_restrictions", form);
response.EnsureSuccessStatusCode();
await File.WriteAllBytesAsync(
"restricted.pdf", await response.Content.ReadAsByteArrayAsync());Antwort
Bei Erfolg lautet die Antwort 200 OK und enthält das eingeschränkte PDF als
Antworttext:
HTTP/1.1 200 OK
Content-Type: application/pdf
Content-Length: 48213Die Ausgabe ist dasselbe Dokument, auf das die angeforderten Berechtigungen angewendet wurden. Seine Seiten und sein Inhalt sind unverändert. Schreiben Sie den Antworttext direkt in eine Datei, wie die Beispiele oben es tun; bei uns wird nichts gespeichert.
Fehler
Fehlgeschlagene Anfragen liefern einen Antworttext vom Typ
application/problem+json. Am häufigsten ist bei diesem Endpoint der Status
400, der zurückgegeben wird, wenn ein Parameter ungültig ist (zum Beispiel ein
owner_password, das nicht aus 4 bis 32 druckbaren ASCII-Zeichen besteht);
dabei nennt das Objekt errors jedes betroffene Feld:
{
"type": "https://www.pdfblocks.com/docs/api/v1/error/400",
"title": "One or more validation errors occurred.",
"status": 400,
"errors": {
"owner_password": ["The field owner_password must match the regular expression '^[\\x20-\\x7e]{4,32}$'."]
}
}Ein fehlender oder ungültiger X-API-Key gibt einen 401 zurück. Alle
Statuscodes und die vollständige Form der Antwort finden Sie unter
Fehler.
Rezepte
Häufige Varianten. Klappen Sie eine auf, um sie in allen Sprachen zu sehen.
Schreibgeschützt: Kopieren, Bearbeiten und Drucken blockieren
curl https://api.pdfblocks.com/v1/add_restrictions \
-H 'X-API-Key: your_api_key' \
-F file=@input.pdf \
-F owner_password='s3cr3t-owner' \
-F allow_copy_content=false \
-F allow_change_content=false \
-F allow_print=false \
-F allow_print_high_resolution=false \
-o readonly.pdfimport requests
with open('input.pdf', 'rb') as file:
response = requests.post(
'https://api.pdfblocks.com/v1/add_restrictions',
headers={'X-API-Key': 'your_api_key'},
files={'file': file},
data={
'owner_password': 's3cr3t-owner',
'allow_copy_content': 'false',
'allow_change_content': 'false',
'allow_print': 'false',
'allow_print_high_resolution': 'false',
},
)
response.raise_for_status()
with open('readonly.pdf', 'wb') as output:
output.write(response.content)import { readFile, writeFile } from 'node:fs/promises';
const body = new FormData();
body.set('file', new Blob([await readFile('input.pdf')]), 'input.pdf');
body.set('owner_password', 's3cr3t-owner');
body.set('allow_copy_content', 'false');
body.set('allow_change_content', 'false');
body.set('allow_print', 'false');
body.set('allow_print_high_resolution', 'false');
const response = await fetch('https://api.pdfblocks.com/v1/add_restrictions', {
method: 'POST',
headers: { 'X-API-Key': 'your_api_key' },
body,
});
if (!response.ok) throw new Error(`Request failed: ${response.status}`);
await writeFile('readonly.pdf', Buffer.from(await response.arrayBuffer()));<?php
$ch = curl_init('https://api.pdfblocks.com/v1/add_restrictions');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['X-API-Key: your_api_key'],
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => [
'file' => new CURLFile('input.pdf', 'application/pdf'),
'owner_password' => 's3cr3t-owner',
'allow_copy_content' => 'false',
'allow_change_content' => 'false',
'allow_print' => 'false',
'allow_print_high_resolution' => 'false',
],
]);
$pdf = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200) {
file_put_contents('readonly.pdf', $pdf);
}require 'http'
response = HTTP
.headers('X-API-Key' => 'your_api_key')
.post('https://api.pdfblocks.com/v1/add_restrictions', form: {
file: HTTP::FormData::File.new('input.pdf'),
owner_password: 's3cr3t-owner',
allow_copy_content: 'false',
allow_change_content: 'false',
allow_print: 'false',
allow_print_high_resolution: 'false',
})
File.write('readonly.pdf', response.body) if response.status.success?package main
import (
"bytes"
"io"
"mime/multipart"
"net/http"
"os"
)
func main() {
var buf bytes.Buffer
form := multipart.NewWriter(&buf)
file, _ := os.Open("input.pdf")
defer file.Close()
part, _ := form.CreateFormFile("file", "input.pdf")
io.Copy(part, file)
form.WriteField("owner_password", "s3cr3t-owner")
form.WriteField("allow_copy_content", "false")
form.WriteField("allow_change_content", "false")
form.WriteField("allow_print", "false")
form.WriteField("allow_print_high_resolution", "false")
form.Close()
req, _ := http.NewRequest("POST", "https://api.pdfblocks.com/v1/add_restrictions", &buf)
req.Header.Set("Content-Type", form.FormDataContentType())
req.Header.Set("X-API-Key", "your_api_key")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
out, _ := os.Create("readonly.pdf")
defer out.Close()
io.Copy(out, res.Body)
}using var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-API-Key", "your_api_key");
using var form = new MultipartFormDataContent
{
{ new ByteArrayContent(File.ReadAllBytes("input.pdf")), "file", "input.pdf" },
{ new StringContent("s3cr3t-owner"), "owner_password" },
{ new StringContent("false"), "allow_copy_content" },
{ new StringContent("false"), "allow_change_content" },
{ new StringContent("false"), "allow_print" },
{ new StringContent("false"), "allow_print_high_resolution" },
};
var response = await client.PostAsync(
"https://api.pdfblocks.com/v1/add_restrictions", form);
response.EnsureSuccessStatusCode();
await File.WriteAllBytesAsync(
"readonly.pdf", await response.Content.ReadAsByteArrayAsync());Passwort zum Öffnen verlangen, mit AES-256
curl https://api.pdfblocks.com/v1/add_restrictions \
-H 'X-API-Key: your_api_key' \
-F file=@input.pdf \
-F owner_password='s3cr3t-owner' \
-F user_password='open-me-2024' \
-F encryption_algorithm=AES-256 \
-o protected.pdfimport requests
with open('input.pdf', 'rb') as file:
response = requests.post(
'https://api.pdfblocks.com/v1/add_restrictions',
headers={'X-API-Key': 'your_api_key'},
files={'file': file},
data={
'owner_password': 's3cr3t-owner',
'user_password': 'open-me-2024',
'encryption_algorithm': 'AES-256',
},
)
response.raise_for_status()
with open('protected.pdf', 'wb') as output:
output.write(response.content)import { readFile, writeFile } from 'node:fs/promises';
const body = new FormData();
body.set('file', new Blob([await readFile('input.pdf')]), 'input.pdf');
body.set('owner_password', 's3cr3t-owner');
body.set('user_password', 'open-me-2024');
body.set('encryption_algorithm', 'AES-256');
const response = await fetch('https://api.pdfblocks.com/v1/add_restrictions', {
method: 'POST',
headers: { 'X-API-Key': 'your_api_key' },
body,
});
if (!response.ok) throw new Error(`Request failed: ${response.status}`);
await writeFile('protected.pdf', Buffer.from(await response.arrayBuffer()));<?php
$ch = curl_init('https://api.pdfblocks.com/v1/add_restrictions');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['X-API-Key: your_api_key'],
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => [
'file' => new CURLFile('input.pdf', 'application/pdf'),
'owner_password' => 's3cr3t-owner',
'user_password' => 'open-me-2024',
'encryption_algorithm' => 'AES-256',
],
]);
$pdf = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200) {
file_put_contents('protected.pdf', $pdf);
}require 'http'
response = HTTP
.headers('X-API-Key' => 'your_api_key')
.post('https://api.pdfblocks.com/v1/add_restrictions', form: {
file: HTTP::FormData::File.new('input.pdf'),
owner_password: 's3cr3t-owner',
user_password: 'open-me-2024',
encryption_algorithm: 'AES-256',
})
File.write('protected.pdf', response.body) if response.status.success?package main
import (
"bytes"
"io"
"mime/multipart"
"net/http"
"os"
)
func main() {
var buf bytes.Buffer
form := multipart.NewWriter(&buf)
file, _ := os.Open("input.pdf")
defer file.Close()
part, _ := form.CreateFormFile("file", "input.pdf")
io.Copy(part, file)
form.WriteField("owner_password", "s3cr3t-owner")
form.WriteField("user_password", "open-me-2024")
form.WriteField("encryption_algorithm", "AES-256")
form.Close()
req, _ := http.NewRequest("POST", "https://api.pdfblocks.com/v1/add_restrictions", &buf)
req.Header.Set("Content-Type", form.FormDataContentType())
req.Header.Set("X-API-Key", "your_api_key")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
out, _ := os.Create("protected.pdf")
defer out.Close()
io.Copy(out, res.Body)
}using var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-API-Key", "your_api_key");
using var form = new MultipartFormDataContent
{
{ new ByteArrayContent(File.ReadAllBytes("input.pdf")), "file", "input.pdf" },
{ new StringContent("s3cr3t-owner"), "owner_password" },
{ new StringContent("open-me-2024"), "user_password" },
{ new StringContent("AES-256"), "encryption_algorithm" },
};
var response = await client.PostAsync(
"https://api.pdfblocks.com/v1/add_restrictions", form);
response.EnsureSuccessStatusCode();
await File.WriteAllBytesAsync(
"protected.pdf", await response.Content.ReadAsByteArrayAsync());