PDF Blocks
料金サポート
無料で始める
ページへ移動

PDF のページを回転する

PDF 内の選択したページを、時計回りまたは反時計回りに固定の角度で回転します。

PDF ドキュメントのページを固定の角度で回転します。正の角度は時計回りに、負の角度は反時計回りに回転します。デフォルトではすべてのページが回転の対象になります。一部のページのみを対象にするには、pages パラメーターを使用してください。この API はステートレスです。ドキュメントはリージョン内で処理され、保存されることはありません。

エンドポイント

POST
/v1/rotate_pages

すべてのリージョンで利用できます。ルーティングとデータの保存地域についてはリージョンとデータの保存地域を参照してください。

リージョン URL
グローバル https://api.pdfblocks.com/v1/rotate_pages
日本 https://jp.api.pdfblocks.com/v1/rotate_pages
米国 https://us.api.pdfblocks.com/v1/rotate_pages
HIPAA 米国 https://hipaa.api.pdfblocks.com/v1/rotate_pages
欧州連合 https://eu.api.pdfblocks.com/v1/rotate_pages
英国 https://uk.api.pdfblocks.com/v1/rotate_pages
カナダ https://ca.api.pdfblocks.com/v1/rotate_pages
オーストラリア https://au.api.pdfblocks.com/v1/rotate_pages
インド https://in.api.pdfblocks.com/v1/rotate_pages
ブラジル https://br.api.pdfblocks.com/v1/rotate_pages

認証

すべてのリクエストは、HTTPS 経由で X-API-Key ヘッダーにシークレット API キーを設定して認証してください。ダッシュボードからキーを作成・管理できます。詳細は認証を参照してください。

リクエスト

このエンドポイントは multipart/form-data 形式のリクエストボディを受け付けます。

filefilerequired

入力する PDF ドキュメント。

angleintegerrequired

適用する回転角度(度単位)。有効な角度のいずれかを指定します。正の値は時計回りに、負の値は反時計回りに回転します。

pagesstring

回転するページを、1..3,5 のようなページ範囲として指定します。省略した場合はすべてのページが回転します。最大1000文字。

回転角度

angle パラメーターには、次のいずれかの値を指定します。

角度 回転
0 回転なし
90 時計回りに90°
180 180°
270 時計回りに270°(反時計回りに90°)
-90 反時計回りに90°
-180 180°
-270 反時計回りに270°(時計回りに90°)

正の角度は時計回りに、負の角度は反時計回りに回転します。各回転は、そのページの現在の回転角度に加算されます。

ページの選択

pages パラメーターには、1から始まるページ番号と範囲をカンマ区切りで指定します。これは集合として扱われるため、順序と重複は無視され、ページは常にドキュメントの順序で回転されます。

パターン 選択されるページ
(省略) すべてのページ
1 最初のページのみ
1..3,5 ページ1、2、3、5
2.. ページ2から最後のページまで
..-2 最初のページから最後から2番目のページまで
-1 最後のページ

完全なリファレンスはページの選択を参照してください。

サンプル

最初の3ページを時計回りに90°回転します。

cURLbash
curl https://api.pdfblocks.com/v1/rotate_pages \
  -H 'X-API-Key: your_api_key' \
  -F file=@input.pdf \
  -F angle=90 \
  -F pages='1..3' \
  -o rotated.pdf
Pythonpython
# pip install requests
import requests

with open('input.pdf', 'rb') as file:
    response = requests.post(
        'https://api.pdfblocks.com/v1/rotate_pages',
        headers={'X-API-Key': 'your_api_key'},
        files={'file': file},
        data={
            'angle': 90,
            'pages': '1..3',
        },
    )

response.raise_for_status()
with open('rotated.pdf', 'wb') as output:
    output.write(response.content)
Node.jsjavascript
// 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('angle', '90');
body.set('pages', '1..3');

const response = await fetch('https://api.pdfblocks.com/v1/rotate_pages', {
  method: 'POST',
  headers: { 'X-API-Key': 'your_api_key' },
  body,
});

if (!response.ok) throw new Error(`Request failed: ${response.status}`);
await writeFile('rotated.pdf', Buffer.from(await response.arrayBuffer()));
PHPphp
<?php
$ch = curl_init('https://api.pdfblocks.com/v1/rotate_pages');
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'),
        'angle' => '90',
        'pages' => '1..3',
    ],
]);

$pdf = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200) {
    file_put_contents('rotated.pdf', $pdf);
}
Rubyruby
# gem install http
require 'http'

response = HTTP
  .headers('X-API-Key' => 'your_api_key')
  .post('https://api.pdfblocks.com/v1/rotate_pages', form: {
    file: HTTP::FormData::File.new('input.pdf'),
    angle: '90',
    pages: '1..3',
  })

File.write('rotated.pdf', response.body) if response.status.success?
Gogo
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("angle", "90")
	form.WriteField("pages", "1..3")
	form.Close()

	req, _ := http.NewRequest("POST", "https://api.pdfblocks.com/v1/rotate_pages", &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("rotated.pdf")
	defer out.Close()
	io.Copy(out, res.Body)
}
C#csharp
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("90"), "angle" },
    { new StringContent("1..3"), "pages" },
};

var response = await client.PostAsync(
    "https://api.pdfblocks.com/v1/rotate_pages", form);
response.EnsureSuccessStatusCode();
await File.WriteAllBytesAsync(
    "rotated.pdf", await response.Content.ReadAsByteArrayAsync());

レスポンス

成功した場合、レスポンスは 200 OK となり、回転後の PDF が本文として返されます。

HTTP/1.1 200 OK
Content-Type: application/pdf
Content-Length: 48213

出力は入力と同じページおよび内容を保持し、選択したページの回転のみが変更されます。上記のサンプルのように、レスポンスボディはそのままファイルへストリーミングしてください。サーバー側には何も保存されません。

エラー

リクエストが失敗した場合、レスポンスボディは application/problem+json 形式で返されます。このエンドポイントで最も多いのは 400 で、angle がサポート対象の値のいずれでもない場合、または file が読み取り可能な PDF でない場合に返されます。errors オブジェクトには各フィールド名が含まれます。

{
  "type": "https://www.pdfblocks.com/docs/api/v1/error/400",
  "title": "One or more validation errors occurred.",
  "status": 400,
  "errors": {
    "angle": ["The angle must be one of 0, 90, 180, 270, -90, -180, -270."]
  }
}

X-API-Key が未指定または無効な場合は 401 が返されます。すべてのステータスコードとレスポンスの完全な形式についてはエラーを参照してください。

レシピ

よく使われるバリエーションです。展開すると、すべての言語でのサンプルを確認できます。

横向きのスキャンを正立させる
cURLbash
curl https://api.pdfblocks.com/v1/rotate_pages \
  -H 'X-API-Key: your_api_key' \
  -F file=@input.pdf \
  -F angle=90 \
  -o upright.pdf
Pythonpython
import requests

with open('input.pdf', 'rb') as file:
    response = requests.post(
        'https://api.pdfblocks.com/v1/rotate_pages',
        headers={'X-API-Key': 'your_api_key'},
        files={'file': file},
        data={'angle': 90},
    )

response.raise_for_status()
with open('upright.pdf', 'wb') as output:
    output.write(response.content)
Node.jsjavascript
import { readFile, writeFile } from 'node:fs/promises';

const body = new FormData();
body.set('file', new Blob([await readFile('input.pdf')]), 'input.pdf');
body.set('angle', '90');

const response = await fetch('https://api.pdfblocks.com/v1/rotate_pages', {
  method: 'POST',
  headers: { 'X-API-Key': 'your_api_key' },
  body,
});

if (!response.ok) throw new Error(`Request failed: ${response.status}`);
await writeFile('upright.pdf', Buffer.from(await response.arrayBuffer()));
PHPphp
<?php
$ch = curl_init('https://api.pdfblocks.com/v1/rotate_pages');
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'),
        'angle' => '90',
    ],
]);

$pdf = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200) {
    file_put_contents('upright.pdf', $pdf);
}
Rubyruby
require 'http'

response = HTTP
  .headers('X-API-Key' => 'your_api_key')
  .post('https://api.pdfblocks.com/v1/rotate_pages', form: {
    file: HTTP::FormData::File.new('input.pdf'),
    angle: '90',
  })

File.write('upright.pdf', response.body) if response.status.success?
Gogo
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("angle", "90")
	form.Close()

	req, _ := http.NewRequest("POST", "https://api.pdfblocks.com/v1/rotate_pages", &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("upright.pdf")
	defer out.Close()
	io.Copy(out, res.Body)
}
C#csharp
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("90"), "angle" },
};

var response = await client.PostAsync(
    "https://api.pdfblocks.com/v1/rotate_pages", form);
response.EnsureSuccessStatusCode();
await File.WriteAllBytesAsync(
    "upright.pdf", await response.Content.ReadAsByteArrayAsync());
すべてのページを180°回転する
cURLbash
curl https://api.pdfblocks.com/v1/rotate_pages \
  -H 'X-API-Key: your_api_key' \
  -F file=@input.pdf \
  -F angle=180 \
  -o flipped.pdf
Pythonpython
import requests

with open('input.pdf', 'rb') as file:
    response = requests.post(
        'https://api.pdfblocks.com/v1/rotate_pages',
        headers={'X-API-Key': 'your_api_key'},
        files={'file': file},
        data={'angle': 180},
    )

response.raise_for_status()
with open('flipped.pdf', 'wb') as output:
    output.write(response.content)
Node.jsjavascript
import { readFile, writeFile } from 'node:fs/promises';

const body = new FormData();
body.set('file', new Blob([await readFile('input.pdf')]), 'input.pdf');
body.set('angle', '180');

const response = await fetch('https://api.pdfblocks.com/v1/rotate_pages', {
  method: 'POST',
  headers: { 'X-API-Key': 'your_api_key' },
  body,
});

if (!response.ok) throw new Error(`Request failed: ${response.status}`);
await writeFile('flipped.pdf', Buffer.from(await response.arrayBuffer()));
PHPphp
<?php
$ch = curl_init('https://api.pdfblocks.com/v1/rotate_pages');
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'),
        'angle' => '180',
    ],
]);

$pdf = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200) {
    file_put_contents('flipped.pdf', $pdf);
}
Rubyruby
require 'http'

response = HTTP
  .headers('X-API-Key' => 'your_api_key')
  .post('https://api.pdfblocks.com/v1/rotate_pages', form: {
    file: HTTP::FormData::File.new('input.pdf'),
    angle: '180',
  })

File.write('flipped.pdf', response.body) if response.status.success?
Gogo
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("angle", "180")
	form.Close()

	req, _ := http.NewRequest("POST", "https://api.pdfblocks.com/v1/rotate_pages", &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("flipped.pdf")
	defer out.Close()
	io.Copy(out, res.Body)
}
C#csharp
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("180"), "angle" },
};

var response = await client.PostAsync(
    "https://api.pdfblocks.com/v1/rotate_pages", form);
response.EnsureSuccessStatusCode();
await File.WriteAllBytesAsync(
    "flipped.pdf", await response.Content.ReadAsByteArrayAsync());

関連アクション