# PDF のページを反転する

PDF のページ順を反転し、最後のページが先頭になるようにします。

PDF のページの順序を反転し、最後のページが先頭に、先頭のページが最後になるようにします。この API はステートレスです。ドキュメントはリージョン内で処理され、保存されることはありません。

<Tip>
  完全な反転だけでなく任意の順序にページを並べ替えるには、[ページの並べ替え](/docs/api/reorder-pages-of-pdf)を使用してください。
</Tip>

## エンドポイント

<Endpoint method="POST" path="/v1/reverse_pages" />

すべてのリージョンで利用できます。ルーティングとデータの保存地域については[リージョンとデータの保存地域](/docs/api/regions-and-data-residency)を参照してください。

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

## 認証

すべてのリクエストは、HTTPS 経由で `X-API-Key` ヘッダーにシークレット API キーを設定して認証してください。[ダッシュボード](https://dashboard.pdfblocks.com)からキーを作成・管理できます。詳細は[認証](/docs/api/authentication)を参照してください。

## リクエスト

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

<ParamField name="file" type="file" required>
  入力する PDF ドキュメント。
</ParamField>

## サンプル

PDF のページ順を反転します。

<CodeGroup>

```bash title="cURL"
curl https://api.pdfblocks.com/v1/reverse_pages \
  -H 'X-API-Key: your_api_key' \
  -F file=@input.pdf \
  -o reversed.pdf
```

```python title="Python"
# pip install requests
import requests

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

response.raise_for_status()
with open('reversed.pdf', 'wb') as output:
    output.write(response.content)
```

```javascript title="Node.js"
// 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');

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

if (!response.ok) throw new Error(`Request failed: ${response.status}`);
await writeFile('reversed.pdf', Buffer.from(await response.arrayBuffer()));
```

```php title="PHP"
<?php
$ch = curl_init('https://api.pdfblocks.com/v1/reverse_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'),
    ],
]);

$pdf = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200) {
    file_put_contents('reversed.pdf', $pdf);
}
```

```ruby title="Ruby"
# gem install http
require 'http'

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

File.write('reversed.pdf', response.body) if response.status.success?
```

```go title="Go"
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.Close()

	req, _ := http.NewRequest("POST", "https://api.pdfblocks.com/v1/reverse_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("reversed.pdf")
	defer out.Close()
	io.Copy(out, res.Body)
}
```

```csharp title="C#"
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" },
};

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

</CodeGroup>

## レスポンス

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

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

出力は各ページの内容と寸法をそのまま保持し、順序のみが反転されます。上記のサンプルのように、レスポンスボディはそのままファイルへストリーミングしてください。サーバー側には何も保存されません。

## エラー

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

```json
{
  "type": "https://www.pdfblocks.com/docs/api/v1/error/400",
  "title": "One or more validation errors occurred.",
  "status": 400,
  "errors": {
    "file": ["Could not parse the PDF document. The file may be invalid or corrupt."]
  }
}
```

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

## 関連アクション

<CardGroup cols={2}>

<Card title="ページの並べ替え" href="/docs/api/reorder-pages-of-pdf">
  反転だけでなく任意の順序に並べ替えます。
</Card>

<Card title="ページの回転" href="/docs/api/rotate-pages-in-pdf">
  固定の角度でページを回転します。
</Card>

<Card title="ページを抽出する" href="/docs/api/extract-pages-from-pdf">
  ページの一部を抽出します。
</Card>

<Card title="ページを削除" href="/docs/api/remove-pages-from-pdf">
  ドキュメントからページを削除します。
</Card>

</CardGroup>
