A Go utility to download and convert books from the Norwegian National Library (Nasjonalbiblioteket) into PDF files.
This tool allows you to download books from the Norwegian National Library's digital collection and convert them to PDF format. It supports both publicly available books (digibok_
prefix) and restricted content (pliktmonografi_
prefix) that requires authentication.
This is a Go port of akselsd/NB.no-Downloader, originally written in Python, with additional features and improvements.
- Download complete books from the Norwegian National Library
- Support for both public (
digibok
) and restricted (pliktmonografi
) document types - Automatically determine book length if not specified
- Authentication support for restricted content (including cookie file support)
- Convert all pages into a single PDF file
- Customizable image quality
- Automatic detection of introduction pages (I1, I2, etc.)
- Go 1.16 or higher
- The following Go packages:
github.com/jung-kurt/gofpdf
(for PDF creation)
- Clone this repository or download the script:
git clone https://github.com/yourusername/nb-book-downloader.git
cd nb-book-downloader
- Install the required dependencies:
go get github.com/jung-kurt/gofpdf
To download a publicly available book:
go run main.go -id 123456789
Or simply:
go run main.go 123456789
To download restricted content using a cookie file (recommended):
go run main.go -id 000040863 -type pliktmonografi -cookie-file cookies.txt
Alternative method with direct cookie string:
go run main.go -id 000040863 -type pliktmonografi -cookies "_nblb=value; nbsso=value; NTID=value"
Flag | Description | Default |
---|---|---|
-id |
Book ID to download | Required |
-type |
Document type: 'digibok' or 'pliktmonografi' | digibok |
-cookie-file |
Path to file containing authentication cookies | "" |
-cookies |
Authentication cookies in 'name1=value1; name2=value2' format | "" |
-length |
Book length (will calculate if not provided) | 0 |
-width |
Image width in pixels for higher quality | 602 |
For restricted content (pliktmonografi), the easiest way to authenticate is with a cookie file:
- Log in to www.nb.no in your browser
- Open developer tools (F12)
- Go to the Network tab and reload the page
- Click on any request to www.nb.no
- In the "Headers" tab, find the "Cookie:" header
- Copy the entire value (without the "Cookie:" prefix)
- Paste it into a text file (e.g.,
cookies.txt
) and save
Example cookie file content:
_nblb=value; nbsso=value; NTID=value; nb_dark_mode_enabled=true
go run main.go -id 123456789
go run main.go -id 000040863 -type pliktmonografi -cookie-file cookies.txt
go run main.go -id 123456789 -length 200
go run main.go -id 123456789 -width 1024
The script will:
- Create a temporary folder to store downloaded images
- Download all pages of the book (including front and back covers)
- Combine all images into a PDF file named
[book-id].pdf
If you receive "401 Unauthorized" or "403 Forbidden" errors with pliktmonografi documents:
- Ensure you're logged in to www.nb.no in your browser when you copy the cookies
- Make sure you've copied the entire cookie string without modifications
- Remember that your session may expire, requiring new cookies
If image downloads fail:
- Check your internet connection
- Verify the book ID is correct
- Ensure the document exists and is accessible with your permissions
- Try with the
-length
parameter if auto-detection fails
- Session cookies expire, so you may need to update them for long downloads
- Very large books may take significant time and disk space
- The National Library may change their API or structure, requiring updates to this tool
This tool is intended for personal use within the terms of service of the Norwegian National Library. Please respect copyright laws and the library's usage policies.
This project is licensed under the MIT License - see the LICENSE file for details.
- akselsd/NB.no-Downloader - The original Python implementation this project is based on
- Norwegian National Library for providing digital access to their collection
- The Go community for excellent libraries and tools