ドキュメントの保護とロック解除
セキュリティ関連の各アクションがどのように連携するか、またドキュメントの保護やロック解除を行う際の呼び出し順序について説明します。
PDF のセキュリティには2つの独立した要素があり、正しい順序で操作するには、そのどちらを扱っているかを見極める必要があります。パスワードはファイルを暗号化し、誰が開けるかを制御します。制限は、開いた後に読み手が何をできるか(印刷、コピー、編集)を制御する権限フラグです。このガイドでは、5つのセキュリティアクションを、保護してから配布する流れと、ロックを解除する流れの2つに整理します。
セキュリティアクション
| アクション | ルート | 内容 |
|---|---|---|
| パスワードの追加 | add_password |
PDF を暗号化し、開くために必要なパスワードを設定します。 |
| 制限の追加 | add_restrictions |
所有者パスワードの背後で権限フラグ(印刷、コピー、編集)を設定します。開くためのパスワードを併せて設定することもできます。 |
| パスワードの削除 | remove_password |
現在のパスワードを指定すると、PDF を復号します。 |
| 制限の削除 | remove_restrictions |
すべての権限フラグをリセットします。 |
| 署名の削除 | remove_signatures |
暗号署名とタイムスタンプを削除します。 |
パスワードと制限。 add_password は単一の password を受け取ります。これは、ファイルを開くために読み手が入力するパスワードです。add_restrictions は、権限フラグを保護する owner_password に加え、開くためのパスワードとして機能する任意の user_password を受け取ります。user_password を設定すると、ドキュメントを開くにはパスワードが必要になります。省略すると誰でも開けますが、権限フラグには引き続き従うことになります。
順序を決める唯一のルール
暗号化は最後に行います。 パスワードを渡すためのフィールドが存在しないため、暗号化済みの PDF は他のどのアクションでも開けません。そのため、コンテンツや権限に関する操作(結合、透かしの付与、制限の設定)はすべて先に実行し、開くためのパスワードは最後のステップとして追加してください。すでにパスワードで保護されているドキュメントを変更するには、まずパスワードを削除してから処理し、その後で保護をかけ直します。
このルールがあるため、add_password を実行してから add_restrictions を重ねて呼び出すことはできません。制限を設定する呼び出しは、暗号化されたばかりのファイルを開けないためです。開くためのパスワードと権限フラグの両方が必要な場合は、owner_password と user_password を組み合わせて、1回の add_restrictions 呼び出しで設定してください。
配布のためにドキュメントを保護する
レポートを1回の呼び出しでロックし、開くためにパスワードを必要とし、印刷、コピー、編集ができないようにします。
ドキュメントがまだ暗号化されていないうちに、結合、透かしの付与、ページ操作などを行います。アクションの連結を参照してください。
POST /v1/add_restrictions に、owner_password(権限を保護する)、user_password(開くためのパスワード)、そして禁止したい操作に対応する権限フラグを false に設定して送信します。
curl https://api.pdfblocks.com/v1/add_restrictions \
-H 'X-API-Key: your_api_key' \
-F file=@input.pdf \
-F owner_password='owner-secret' \
-F user_password='open-secret' \
-F allow_print=false \
-F allow_copy_content=false \
-F allow_change_content=false \
-o protected.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': 'owner-secret',
'user_password': 'open-secret',
'allow_print': 'false',
'allow_copy_content': 'false',
'allow_change_content': 'false',
},
)
response.raise_for_status()
with open('protected.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', 'owner-secret');
body.set('user_password', 'open-secret');
body.set('allow_print', 'false');
body.set('allow_copy_content', 'false');
body.set('allow_change_content', '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('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' => 'owner-secret',
'user_password' => 'open-secret',
'allow_print' => 'false',
'allow_copy_content' => 'false',
'allow_change_content' => 'false',
],
]);
$pdf = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200) {
file_put_contents('protected.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: 'owner-secret',
user_password: 'open-secret',
allow_print: 'false',
allow_copy_content: 'false',
allow_change_content: 'false',
})
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", "owner-secret")
form.WriteField("user_password", "open-secret")
form.WriteField("allow_print", "false")
form.WriteField("allow_copy_content", "false")
form.WriteField("allow_change_content", "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("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("owner-secret"), "owner_password" },
{ new StringContent("open-secret"), "user_password" },
{ new StringContent("false"), "allow_print" },
{ new StringContent("false"), "allow_copy_content" },
{ new StringContent("false"), "allow_change_content" },
};
var response = await client.PostAsync(
"https://api.pdfblocks.com/v1/add_restrictions", form);
response.EnsureSuccessStatusCode();
await File.WriteAllBytesAsync(
"protected.pdf", await response.Content.ReadAsByteArrayAsync());開くためのパスワードのみが必要で、権限フラグが不要な場合は、add_password を使うほうがシンプルです:
curl https://api.pdfblocks.com/v1/add_password \
-H 'X-API-Key: your_api_key' \
-F file=@input.pdf \
-F password='pa$$word' \
-o encrypted.pdfadd_password と add_restrictions はどちらも、encryption_algorithm として AES-128(デフォルト)または AES-256 を受け付けます。すべてのフィールドについては、パスワードの追加と制限の追加を参照してください。
ドキュメントのロックを解除する
認証情報を持っている保護を解除するには、まず復号してから権限フラグをリセットします。
POST /v1/remove_password に、ドキュメントの現在の password を指定します。結果として得られる PDF は開くためのパスワードを必要としなくなるため、以降のステップで読み取れます。
POST /v1/remove_restrictions は権限フラグを削除し、制限のない PDF を残します。
# 1. Decrypt with the known password.
curl -sS https://api.pdfblocks.com/v1/remove_password \
-H 'X-API-Key: your_api_key' \
-F file=@protected.pdf \
-F password='open-secret' |
# 2. Clear the permission flags from the decrypted bytes.
curl -sS https://api.pdfblocks.com/v1/remove_restrictions \
-H 'X-API-Key: your_api_key' \
-F 'file=@-;filename=unlocked.pdf;type=application/pdf' \
-o unrestricted.pdf# pip install requests
import requests
BASE = 'https://api.pdfblocks.com'
HEADERS = {'X-API-Key': 'your_api_key'}
# 1. Decrypt with the known password.
with open('protected.pdf', 'rb') as file:
unlocked = requests.post(
f'{BASE}/v1/remove_password',
headers=HEADERS,
files={'file': file},
data={'password': 'open-secret'},
)
unlocked.raise_for_status()
# 2. Clear the permission flags from the decrypted bytes.
unrestricted = requests.post(
f'{BASE}/v1/remove_restrictions',
headers=HEADERS,
files={'file': ('unlocked.pdf', unlocked.content, 'application/pdf')},
)
unrestricted.raise_for_status()
with open('unrestricted.pdf', 'wb') as output:
output.write(unrestricted.content)remove_restrictions にはパスワードフィールドがないため、制限はあるがパスワードで保護されていないドキュメントに対しても機能します。ドキュメントに開くためのパスワードが設定されている場合は、上記の流れのように、まず remove_password を実行してから、制限の呼び出しがそれを読み取れるようにしてください。
署名を削除する
暗号署名は PDF をロックし、どのような編集を加えても署名が無効になります。署名済みのドキュメントを再処理する必要がある場合は、まず remove_signatures で署名とタイムスタンプを削除してください。これにより署名は無効になりますが、内容が変更される以上、それは避けられません。
curl https://api.pdfblocks.com/v1/remove_signatures \
-H 'X-API-Key: your_api_key' \
-F file=@signed.pdf \
-o unsigned.pdf