Files

140 lines
3.6 KiB
Markdown

# Kramerius PDF Downloader
Download page images from Digitalniknihovna / Kramerius and assemble them into a PDF.
The script:
- discovers all page UUIDs for a document
- downloads each page image
- embeds the downloaded JPEGs into a PDF
- can resume interrupted downloads by skipping existing images
- supports authenticated downloads with a Kramerius bearer token
- can resolve other `digitalniknihovna.cz/<library>/...` links automatically
## Requirements
Python 3.10 or newer is recommended. The script uses only the Python standard library, so no `pip install` step is needed.
## Basic Usage
```bash
python3 download_pdf.py "https://www.digitalniknihovna.cz/mzk/view/uuid:0abf59c0-794e-11e4-8ce5-005056827e52?page=uuid:e57e2820-8547-11e4-a321-5ef3fc9bb22f" \
--output-dir pages \
--pdf book.pdf
```
To test only the first few pages:
```bash
python3 download_pdf.py "URL_HERE" \
--limit 20 \
--output-dir test_pages \
--pdf test.pdf
```
If a run is interrupted, run the same command again. Existing images are skipped unless you pass `--overwrite`.
## Authenticated Downloads
Some pages require a logged-in account. The Digitalniknihovna web app sends protected image requests with:
```text
Authorization: Bearer <token>
Client: www.digitalniknihovna.cz
```
Use either `--auth-token`:
```bash
python3 download_pdf.py "URL_HERE" \
--auth-token "PASTE_TOKEN_HERE" \
--output-dir pages \
--pdf book.pdf
```
Or put the token in `auth-token.txt` and use `--auth-file`:
```bash
python3 download_pdf.py "URL_HERE" \
--auth-file auth-token.txt \
--output-dir pages \
--pdf book.pdf
```
`auth-token.txt` may contain either the raw token or a copied header like:
```text
Authorization: Bearer eyJ...
```
## How To Get The Auth Token
1. Log in on `https://www.digitalniknihovna.cz`.
2. Open the protected book page in the browser.
3. Open browser developer tools:
- Chrome / Edge: `F12` or `Ctrl+Shift+I`
- Firefox: `F12`
4. Go to the **Application** tab.
5. Open **Local storage** in the left sidebar.
6. Select:
```text
https://www.digitalniknihovna.cz
```
7. Find the key for your library. For MZK it is:
```text
auth.token.mzk
```
8. Copy the value and save it in `auth-token.txt`, or pass it directly with `--auth-token`.
For another library, the key usually follows the same pattern:
```text
auth.token.<library-code>
```
For example, a URL containing `/mzk/view/...` uses `auth.token.mzk`.
## Using Other Digitalniknihovna URLs
Paste the document URL directly:
```bash
python3 download_pdf.py "https://www.digitalniknihovna.cz/LIBRARY_CODE/view/uuid:..." \
--auth-file auth-token.txt \
--output-dir pages \
--pdf book.pdf
```
The script reads the library code from the URL and tries to resolve the correct Kramerius API backend automatically. If that fails, pass the API backend manually:
```bash
python3 download_pdf.py "URL_HERE" \
--library mzk \
--base-url "https://api.kramerius.mzk.cz" \
--auth-file auth-token.txt \
--output-dir pages \
--pdf book.pdf
```
## Useful Options
```text
--limit N Download only the first N pages
--overwrite Redownload images that already exist
--delay SECONDS Wait between image downloads
--cookies FILE Send cookies from a cookies.txt export
--auth-token TOKEN Send Authorization: Bearer TOKEN
--auth-file FILE Read token or headers from a file
--client VALUE Override the Client header
```
## Security Notes
Treat `auth-token.txt`, `cookie-dl.txt`, and copied request headers like passwords. Do not share them and do not commit them to version control.
This repository's `.gitignore` already ignores common token, cookie, header, PDF, and downloaded-page files.