PDF Blocks
PrezziSupporto
Iniziare gratis
Aprire la pagina

Invertire le pagine di un PDF

Invertire l’ordine delle pagine di un PDF in modo che l’ultima pagina diventi la prima.

Invertire l’ordine delle pagine di un PDF: l’ultima pagina diventa la prima e la prima diventa l’ultima. L’API è stateless: il documento viene elaborato nella regione e non viene mai memorizzato.

Per riorganizzare le pagine in un ordine qualsiasi, e non solo per invertirle completamente, utilizzare Riordinare le pagine.

Endpoint

POST
/v1/reverse_pages

Disponibile in tutte le regioni. Vedere Regioni e residenza dei dati per il routing e la residenza dei dati.

Regione URL
Globale https://api.pdfblocks.com/v1/reverse_pages
Stati Uniti https://us.api.pdfblocks.com/v1/reverse_pages
HIPAA Stati Uniti https://hipaa.api.pdfblocks.com/v1/reverse_pages
Unione europea https://eu.api.pdfblocks.com/v1/reverse_pages
Regno Unito https://uk.api.pdfblocks.com/v1/reverse_pages
Canada https://ca.api.pdfblocks.com/v1/reverse_pages
Australia https://au.api.pdfblocks.com/v1/reverse_pages
Giappone https://jp.api.pdfblocks.com/v1/reverse_pages
India https://in.api.pdfblocks.com/v1/reverse_pages
Brasile https://br.api.pdfblocks.com/v1/reverse_pages

Autenticazione

Autenticare ogni richiesta con la chiave API segreta nell’intestazione X-API-Key, tramite HTTPS. Le chiavi si creano e si gestiscono dalla dashboard. Vedere Autenticazione per i dettagli.

Richiesta

L’endpoint accetta un corpo della richiesta multipart/form-data.

filefilerequired

Il documento PDF di input.

Esempi

Invertire l’ordine delle pagine di un PDF:

cURLbash
curl https://api.pdfblocks.com/v1/reverse_pages \
  -H 'X-API-Key: your_api_key' \
  -F file=@input.pdf \
  -o reversed.pdf
Pythonpython
# 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)
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');

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()));
PHPphp
<?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);
}
Rubyruby
# 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?
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.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)
}
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" },
};

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

Risposta

In caso di successo, la risposta è 200 OK e il corpo contiene il PDF invertito:

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

L’output conserva il contenuto e le dimensioni di ogni pagina: viene invertito solo il loro ordine. Scrivere il corpo direttamente in un file, come fanno gli esempi qui sopra; dalla nostra parte non viene memorizzato nulla.

Errori

Le richieste non riuscite restituiscono un corpo application/problem+json. L’errore più frequente su questo endpoint è un 400, restituito quando file non è un PDF leggibile. L’oggetto errors nomina ogni campo:

{
  "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."]
  }
}

Una X-API-Key assente o non valida restituisce un 401. Vedere Errori per tutti i codici di stato e la forma completa della risposta.

Azioni correlate