# Seiten aus einem PDF entfernen

Entfernen Sie eine Auswahl von Seiten aus einem PDF. Mindestens eine Seite bleibt immer erhalten.

Entfernen Sie eine oder mehrere Seiten aus einem PDF-Dokument. Wählen Sie die
zu verwerfenden Seiten mit dem Parameter [`pages`](#seiten-auswählen) aus; da
mindestens eine Seite erhalten bleiben muss, darf die Auswahl nicht alle Seiten
abdecken. Die API ist *stateless*: Ihr Dokument wird in der Region
verarbeitet und niemals gespeichert.

## Endpoint

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

In allen Regionen verfügbar. Hinweise zu Routing und Datenresidenz finden Sie
unter [Regionen und Datenresidenz](/docs/api/regions-and-data-residency).

| Region                 | URL                                                 |
| ---------------------- | --------------------------------------------------- |
| Global                 | `https://api.pdfblocks.com/v1/remove_pages`         |
| USA                    | `https://us.api.pdfblocks.com/v1/remove_pages`      |
| HIPAA USA              | `https://hipaa.api.pdfblocks.com/v1/remove_pages`   |
| Europäische Union      | `https://eu.api.pdfblocks.com/v1/remove_pages`      |
| Vereinigtes Königreich | `https://uk.api.pdfblocks.com/v1/remove_pages`      |
| Kanada                 | `https://ca.api.pdfblocks.com/v1/remove_pages`      |
| Australien             | `https://au.api.pdfblocks.com/v1/remove_pages`      |
| Japan                  | `https://jp.api.pdfblocks.com/v1/remove_pages`      |
| Indien                 | `https://in.api.pdfblocks.com/v1/remove_pages`      |
| Brasilien              | `https://br.api.pdfblocks.com/v1/remove_pages`      |

## 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](https://dashboard.pdfblocks.com). Einzelheiten finden Sie unter
[Authentifizierung](/docs/api/authentication).

## Anfrage

Der Endpoint nimmt einen Anfragetext vom Typ `multipart/form-data` entgegen.

<ParamField name="file" type="file" required>
  Das PDF-Eingabedokument.
</ParamField>

<ParamField name="pages" type="string" required>
  Die zu entfernenden Seiten, geschrieben als
  [Seitenbereich](#seiten-auswählen) wie `2,4..6`. Die Auswahl darf nicht alle
  Seiten abdecken: Mindestens eine Seite muss erhalten bleiben. Maximal 1.000
  Zeichen.
</ParamField>

### Seiten auswählen

Der Parameter `pages` nimmt eine durch Kommas getrennte Liste aus 1-basierten
Seitenzahlen und Bereichen entgegen. Er wird als **Menge** behandelt:
Reihenfolge und Duplikate werden ignoriert, und die verbleibenden Seiten
behalten ihre ursprüngliche Reihenfolge im Dokument.

| Muster    | Entfernt                                   |
| --------- | ------------------------------------------ |
| `1`       | Nur die erste Seite                        |
| `1..3,5`  | Die Seiten 1, 2, 3 und 5                   |
| `2..`     | Von Seite 2 bis zur letzten Seite          |
| `..-2`    | Von der ersten bis zur vorletzten Seite    |
| `-1`      | Die letzte Seite                           |

Siehe [Seiten auswählen](/docs/api/selecting-pages) für die vollständige
Referenz.

<Note>
  Die Auswahl ist eine **Menge**, und sie muss mindestens eine Seite übrig
  lassen. Eine Auswahl, die alle Seiten abdeckt, wird mit `400` abgelehnt.
</Note>

## Beispiele

Entfernen Sie Seite 2 und die Seiten 4 bis 6 aus einem PDF:

<CodeGroup>

```bash title="cURL"
curl https://api.pdfblocks.com/v1/remove_pages \
  -H 'X-API-Key: your_api_key' \
  -F file=@input.pdf \
  -F pages='2,4..6' \
  -o trimmed.pdf
```

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

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

response.raise_for_status()
with open('trimmed.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');
body.set('pages', '2,4..6');

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

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

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

$pdf = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200) {
    file_put_contents('trimmed.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/remove_pages', form: {
    file: HTTP::FormData::File.new('input.pdf'),
    pages: '2,4..6',
  })

File.write('trimmed.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.WriteField("pages", "2,4..6")
	form.Close()

	req, _ := http.NewRequest("POST", "https://api.pdfblocks.com/v1/remove_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("trimmed.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" },
    { new StringContent("2,4..6"), "pages" },
};

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

</CodeGroup>

## Antwort

Bei Erfolg lautet die Antwort `200 OK` mit dem gekürzten PDF als Antworttext:

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

Die verbleibenden Seiten behalten ihre ursprüngliche Reihenfolge: Nur die
ausgewählten Seiten werden verworfen. Schreiben Sie den Antworttext direkt in
eine Datei, wie es die Beispiele oben tun; auf unserer Seite wird nichts
gespeichert.

## Fehler

Fehlgeschlagene Anfragen geben einen Text vom Typ `application/problem+json`
zurück. Der häufigste Fehler an diesem Endpoint ist `400`, der zurückgegeben
wird, wenn `pages` fehlerhaft ist oder alle Seiten entfernen würde. Das Objekt
`errors` benennt jedes Feld:

```json
{
  "type": "https://www.pdfblocks.com/docs/api/v1/error/400",
  "title": "One or more validation errors occurred.",
  "status": 400,
  "errors": {
    "pages": ["At least one page must remain, so the selection cannot cover every page."]
  }
}
```

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](/docs/api/errors).

## Rezepte

Häufige Varianten. Klappen Sie eine auf, um sie in allen Sprachen zu sehen.

<AccordionGroup>

<Accordion title="Die letzte Seite verwerfen">

<CodeGroup>

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

```python title="Python"
import requests

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

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

```javascript title="Node.js"
import { readFile, writeFile } from 'node:fs/promises';

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

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

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

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

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

```ruby title="Ruby"
require 'http'

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

File.write('without-last.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.WriteField("pages", "-1")
	form.Close()

	req, _ := http.NewRequest("POST", "https://api.pdfblocks.com/v1/remove_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("without-last.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" },
    { new StringContent("-1"), "pages" },
};

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

</CodeGroup>

</Accordion>

<Accordion title="Das Deckblatt entfernen">

<CodeGroup>

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

```python title="Python"
import requests

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

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

```javascript title="Node.js"
import { readFile, writeFile } from 'node:fs/promises';

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

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

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

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

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

```ruby title="Ruby"
require 'http'

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

File.write('no-cover.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.WriteField("pages", "1")
	form.Close()

	req, _ := http.NewRequest("POST", "https://api.pdfblocks.com/v1/remove_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("no-cover.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" },
    { new StringContent("1"), "pages" },
};

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

</CodeGroup>

</Accordion>

</AccordionGroup>

## Verwandte Aktionen

<CardGroup cols={2}>

<Card title="Seiten extrahieren" href="/docs/api/extract-pages-from-pdf">
  Seiten behalten, statt sie zu verwerfen.
</Card>

<Card title="Seiten neu anordnen" href="/docs/api/reorder-pages-of-pdf">
  Die verbleibenden Seiten neu anordnen.
</Card>

<Card title="Seiten umkehren" href="/docs/api/reverse-pages-of-pdf">
  Die Seitenreihenfolge umkehren.
</Card>

<Card title="Seiten drehen" href="/docs/api/rotate-pages-in-pdf">
  Ausgewählte Seiten drehen.
</Card>

</CardGroup>
