PDF に制限を追加する
所有者パスワードによって保護された PDF の権限フラグを設定し、コピー、印刷、編集、フォームへの入力を制御します。
PDF に権限の制限を適用し(コピー、印刷、編集、フォームへの入力などを制限)、owner_password によってこれを適用します。制限は権限フラグであり、ファイルを開くために必要なパスワードのみを設定するパスワードの追加とは異なります。ここで user_password を設定すれば、両方を要求することもできます。API はステートレスです。ドキュメントはリージョン内で処理され、保存されることはありません。
エンドポイント
/v1/add_restrictionsすべてのリージョンで利用できます。ルーティングとデータレジデンシーについては、リージョンとデータレジデンシーを参照してください。
| リージョン | URL |
|---|---|
| グローバル | https://api.pdfblocks.com/v1/add_restrictions |
| 日本 | https://jp.api.pdfblocks.com/v1/add_restrictions |
| 米国 | https://us.api.pdfblocks.com/v1/add_restrictions |
| HIPAA 米国 | https://hipaa.api.pdfblocks.com/v1/add_restrictions |
| 欧州連合 | https://eu.api.pdfblocks.com/v1/add_restrictions |
| 英国 | https://uk.api.pdfblocks.com/v1/add_restrictions |
| カナダ | https://ca.api.pdfblocks.com/v1/add_restrictions |
| オーストラリア | https://au.api.pdfblocks.com/v1/add_restrictions |
| インド | https://in.api.pdfblocks.com/v1/add_restrictions |
| ブラジル | https://br.api.pdfblocks.com/v1/add_restrictions |
認証
すべてのリクエストは、HTTPS 経由で X-API-Key ヘッダーにシークレット API キーを設定して認証してください。キーの作成と管理はダッシュボードから行います。詳細は認証を参照してください。
リクエスト
このエンドポイントは multipart/form-data 形式のリクエストボディを受け付けます。
filefilerequired入力する PDF ドキュメント。
owner_passwordstringrequired所有者パスワード。4〜32文字の印字可能な ASCII 文字。ドキュメントを開き、その 権限フラグを変更します。
user_passwordstring省略可能なユーザーパスワード。4〜32文字の印字可能な ASCII 文字。設定すると、このパスワードなしではドキュメントを開けなくなります。省略するか空のままにすると、誰でもドキュメントを開けるようになります。
encryption_algorithmstringdefault:AES-128暗号化アルゴリズム。AES-128 または AES-256 のいずれか。
allow_copy_contentbooleandefault:trueテキストと画像をクリップボードにコピーすることを許可します。
allow_change_contentbooleandefault:trueドキュメントの内容を変更することを許可します。
allow_printbooleandefault:trueドキュメントの印刷を許可します。
allow_print_high_resolutionbooleandefault:true高解像度での印刷を許可します。
allow_comment_and_fill_formbooleandefault:true注釈の追加、編集、変更、およびフォームフィールドへの入力を許可します。
allow_fill_formbooleandefault:trueフォームフィールドへの入力を許可します。
allow_assemble_documentbooleandefault:trueドキュメントの結合や操作(ページの挿入、削除、回転)を許可します。
allow_accessibilitybooleandefault:trueアクセシビリティソフトウェアがドキュメントのテキストと画像を読み取ることを許可します。
権限フラグ
各フラグは、読者が実行できる1つの操作を制御します。フラグは真偽値として渡します。その権限をオフにするには false を設定してください。
| フラグ | デフォルト | false に設定すると… |
|---|---|---|
allow_copy_content |
true |
テキストと画像のクリップボードへのコピーをブロックする |
allow_change_content |
true |
ドキュメントの内容の変更をブロックする |
allow_print |
true |
ドキュメントの印刷をブロックする |
allow_print_high_resolution |
true |
高解像度での印刷をブロックする |
allow_comment_and_fill_form |
true |
注釈の追加、編集、変更およびフォームフィールドへの入力をブロックする |
allow_fill_form |
true |
フォームフィールドへの入力をブロックする |
allow_assemble_document |
true |
ドキュメントの結合や操作(ページの挿入、削除、回転)をブロックする |
allow_accessibility |
true |
アクセシビリティソフトウェアによるドキュメントの読み取りをブロックする |
すべてのフラグはデフォルトで true になっているため、指定しない限り何も制限されません。オフにしたいフラグだけを送信してください。制限は owner_password によって適用されます。それを知っている読者は、権限を変更できます。ファイルを開くためにもパスワードを要求したい場合にのみ、user_password を追加してください。ライフサイクル全体については、ドキュメントの保護を参照してください。
例
コピーや印刷ができないようにドキュメントをロックしつつ、誰でも開けるようにする例:
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());レスポンス
成功すると、制限が適用された PDF を本文とする 200 OK が返されます:
HTTP/1.1 200 OK
Content-Type: application/pdf
Content-Length: 48213出力は、指定した権限が適用された同じドキュメントです。ページと内容は変更されません。上記の例のように、レスポンスの本文はそのままファイルにストリーミングしてください。サーバー側には何も保存されません。
エラー
リクエストが失敗した場合は、application/problem+json 形式のボディが返されます。このエンドポイントで最も多いのは 400 で、パラメーターが無効な場合(たとえば owner_password が4〜32文字の印字可能な ASCII 文字でない場合)に返され、errors オブジェクトに該当するフィールド名が示されます:
{
"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}$'."]
}
}X-API-Key が指定されていないか無効な場合は 401 が返されます。すべてのステータスコードとレスポンスの完全な形式については、エラーを参照してください。
レシピ
よく使われるバリエーションです。展開すると、各言語のコード例を確認できます。
読み取り専用:コピー、編集、印刷をブロック
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());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());