Generating compact, printable PDFs from multiple pages is a frequent need for reports, catalogs, and manuals. Aspose.PDF for Python via .NET provides a robust SDK that lets you programmatically create PDF Booklet in Python with fine-grained control. In this guide you’ll see how to set up the environment, build a booklet, optionally add bookmarks, and export the final document.
What is a PDF Booklet?
A PDF booklet is a document arranged so that multiple pages can be printed, folded, and read like a small book, manual, brochure, or catalog. Instead of keeping pages in normal sequential order, booklet generation rearranges pages so that the correct pages appear side by side after printing and folding.
For example, a booklet layout may place one page on the left side of a sheet and another page on the right side. When printed double-sided and folded, the pages appear in the correct reading order.
Booklets are commonly used for:
- Product catalogs
- Event programs
- Training manuals
- Reports
- Instruction guides
- Company profiles
- Brochures
- Small printable books
In Python, you can automate this process instead of manually rearranging pages in a PDF editor.
Python PDF Booklet Generator API
To create a PDF booklet in Python, we will use Aspose.PDF for Python via .NET. It is a PDF processing API that allows Python developers to create, edit, convert, split, merge, secure, annotate, and manipulate PDF files programmatically.
For booklet creation, the key class is:
pdf.facades.PdfFileEditor()
The PdfFileEditor class provides high-level PDF file manipulation features. In this tutorial, we will use its make_booklet() method to convert an existing PDF into a booklet layout.
Step 1: Install Aspose.PDF SDK
First, install Aspose.PDF for Python via .NET using pip:
pip install aspose-pdf
Step 2: Prepare the Input PDF
Before creating a booklet, make sure you already have a source PDF file. For this tutorial, we will use the following file name:
input.pdf
The output booklet will be saved as:
booklet.pdf
Place the input PDF in the same directory as your Python script, or provide the full file path.
Example:
project-folder/
│
├── input.pdf
└── create_booklet.py
Step 3: Create a Simple PDF Booklet in Python
The simplest way to create a booklet is to call make_booklet() with the input file, output file, and target page size.
The following Python code creates a booklet using A5 page size:
import aspose.pdf as pdf
# Create PdfFileEditor object
pdf_editor = pdf.facades.PdfFileEditor()
# Create booklet from input PDF
pdf_editor.make_booklet(
"input.pdf",
"booklet.pdf",
pdf.PageSize.a5
)
print("PDF booklet created successfully.")
This code loads input.pdf, arranges its pages into a booklet layout, and saves the result as booklet.pdf.
Step 4: Create a Custom PDF Booklet with Left and Right Pages
In many cases, you may want to control which pages appear on the left side and which pages appear on the right side of the booklet layout.
The following example is based on the original C# code and converts it into Python:
import aspose.pdf as pdf
# Create PdfFileEditor object
pdf_editor = pdf.facades.PdfFileEditor()
# Set left and right pages
left_pages = [1, 5]
right_pages = [2, 3]
# Make booklet
pdf_editor.make_booklet(
"input.pdf",
"booklet.pdf",
pdf.PageSize.a5,
left_pages,
right_pages
)
print("PDF booklet created successfully.")
Code Explanation
Let’s break down the code.
First, import the Aspose.PDF package:
import aspose.pdf as pdf
Then create an instance of PdfFileEditor:
pdf_editor = pdf.facades.PdfFileEditor()
This object provides access to high-level PDF editing features, including booklet creation.
Next, define the page arrangement:
left_pages = [1, 5]
right_pages = [2, 3]
The left_pages list defines the pages that will appear on the left side of the booklet spread. The right_pages list defines the pages that will appear on the right side.
Finally, call the make_booklet() method:
pdf_editor.make_booklet(
"input.pdf",
"booklet.pdf",
pdf.PageSize.a5,
left_pages,
right_pages
)
This creates the booklet and saves it as booklet.pdf.
Complete Python Code
Here is the complete code example for creating a PDF booklet in Python:
import aspose.pdf as pdf
def create_pdf_booklet(input_pdf: str, output_pdf: str) -> None:
"""
Create a PDF booklet from an existing PDF file using Aspose.PDF for Python via .NET.
Args:
input_pdf: Path to the source PDF file.
output_pdf: Path where the generated booklet PDF will be saved.
"""
# Create PdfFileEditor object
pdf_editor = pdf.facades.PdfFileEditor()
# Set left and right pages
left_pages = [1, 5]
right_pages = [2, 3]
# Make booklet
pdf_editor.make_booklet(
input_pdf,
output_pdf,
pdf.PageSize.a5,
left_pages,
right_pages
)
if __name__ == "__main__":
create_pdf_booklet(
input_pdf="input.pdf",
output_pdf="booklet.pdf"
)
print("PDF booklet created successfully.")
Step 5: Add Optional Bookmarks to the Booklet
After creating the booklet, you may want to add bookmarks to make navigation easier. Bookmarks are useful for reports, manuals, catalogs, training guides, and other long documents.
The following example first creates a booklet and then adds bookmarks to the generated PDF:
import aspose.pdf as pdf
def create_pdf_booklet(input_pdf: str, output_pdf: str) -> None:
"""
Create a PDF booklet from an existing PDF file.
"""
pdf_editor = pdf.facades.PdfFileEditor()
left_pages = [1, 5]
right_pages = [2, 3]
pdf_editor.make_booklet(
input_pdf,
output_pdf,
pdf.PageSize.a5,
left_pages,
right_pages
)
def add_bookmarks(input_pdf: str, output_pdf: str) -> None:
"""
Add bookmarks to an existing PDF document.
"""
# Open generated booklet PDF
document = pdf.Document(input_pdf)
# Add bookmark for first page
first_bookmark = pdf.OutlineItemCollection(document.outlines)
first_bookmark.title = "Booklet Start"
first_bookmark.bold = True
first_bookmark.action = pdf.annotations.GoToAction(document.pages[1])
document.outlines.append(first_bookmark)
# Add bookmark for second page, if available
if len(document.pages) >= 2:
second_bookmark = pdf.OutlineItemCollection(document.outlines)
second_bookmark.title = "Second Spread"
second_bookmark.action = pdf.annotations.GoToAction(document.pages[2])
document.outlines.append(second_bookmark)
# Save PDF with bookmarks
document.save(output_pdf)
if __name__ == "__main__":
create_pdf_booklet(
input_pdf="input.pdf",
output_pdf="booklet.pdf"
)
add_bookmarks(
input_pdf="booklet.pdf",
output_pdf="booklet-with-bookmarks.pdf"
)
print("PDF booklet with bookmarks created successfully.")
This code creates two output files:
booklet.pdf
booklet-with-bookmarks.pdf
The first file contains the booklet layout. The second file contains the booklet with added bookmarks.
Choosing the Right Page Size
In the examples above, we used A5 page size:
pdf.PageSize.a5
A5 is commonly used for booklets because it is compact and suitable for printed manuals, brochures, and small catalogs.
You can use other page sizes depending on your output requirements. For example, if you are creating a larger booklet, you may use A4. If you are preparing a compact manual, A5 may be more suitable.
Common booklet use cases include:
| Use Case | Suggested Page Size |
|---|---|
| Small manual | A5 |
| Product catalog | A5 or A4 |
| Event program | A5 |
| Training handout | A4 or A5 |
| Company brochure | A5 |
Best Practices for Creating PDF Booklets
When generating booklets programmatically, keep the following points in mind.
1. Use a Clean Source PDF
Your input PDF should already be properly formatted. Booklet generation rearranges pages, but it does not rewrite the internal content of each page.
Before creating the booklet, check:
- Page margins
- Header and footer placement
- Font embedding
- Image quality
- Page orientation
- Bleed or print-safe areas
2. Check Page Count
Booklets work best when the page count is suitable for folding and printing. If the page count does not align perfectly, you may need to add blank pages.
For example, a booklet may require pages in multiples of four because each folded sheet produces four page sides.
3. Test Print Before Bulk Printing
Always print a test copy before sending the booklet for bulk printing. PDF viewers and printer settings can affect scaling, margins, duplex mode, and page orientation.
Recommended printer settings may include:
- Print on both sides
- Flip on short edge or long edge, depending on the printer
- Actual size
- No automatic scaling
- Correct paper size
4. Add Bookmarks for Digital Copies
If the booklet will also be shared digitally, bookmarks improve navigation. They help readers jump to important sections quickly.
Bookmarks are especially useful for:
- Long reports
- Technical manuals
- Policy documents
- Product catalogs
- Training material
5. Use Clear File Names
Use descriptive names for output files, especially when generating documents in batch mode.
For example:
employee-handbook-booklet.pdf
product-catalog-booklet.pdf
training-manual-booklet.pdf
Common Errors and Fixes
Error: ModuleNotFoundError: No module named ‘aspose’
This means the Aspose.PDF package is not installed in your current Python environment.
Fix it with:
pip install aspose-pdf
If you are using a virtual environment, make sure it is activated before installing the package.
Error: Input PDF Not Found
If your script cannot find the input file, check the file path.
Instead of:
"input.pdf"
Use an absolute path:
"C:/Users/YourName/Documents/input.pdf"
Or on Linux/macOS:
"/home/user/documents/input.pdf"
Error: Booklet Output is Not in Expected Order
If you are using custom left and right page arrays, verify that the page numbers match the pages in the source PDF.
Example:
left_pages = [1, 5]
right_pages = [2, 3]
These values should be selected based on your desired booklet spread arrangement.
Error: Bookmarks Do Not Appear
If bookmarks do not appear in your PDF viewer, check whether the viewer has the bookmark panel enabled. Some PDF viewers hide bookmarks by default.
You can also set the document page mode to show outlines:
document.page_mode = pdf.PageMode.USE_OUTLINES
Add this before saving the PDF:
document.save(output_pdf)
Create PDF Booklet in Python: Final Code
The following final code creates a PDF booklet, adds bookmarks, and saves the result:
import aspose.pdf as pdf
def create_pdf_booklet(input_pdf: str, booklet_pdf: str) -> None:
"""
Create a custom PDF booklet using Aspose.PDF for Python via .NET.
"""
pdf_editor = pdf.facades.PdfFileEditor()
left_pages = [1, 5]
right_pages = [2, 3]
pdf_editor.make_booklet(
input_pdf,
booklet_pdf,
pdf.PageSize.a5,
left_pages,
right_pages
)
def add_bookmarks_to_booklet(booklet_pdf: str, output_pdf: str) -> None:
"""
Add bookmarks to the generated booklet PDF.
"""
document = pdf.Document(booklet_pdf)
start_bookmark = pdf.OutlineItemCollection(document.outlines)
start_bookmark.title = "Booklet Start"
start_bookmark.bold = True
start_bookmark.action = pdf.annotations.GoToAction(document.pages[1])
document.outlines.append(start_bookmark)
if len(document.pages) >= 2:
second_bookmark = pdf.OutlineItemCollection(document.outlines)
second_bookmark.title = "Second Spread"
second_bookmark.action = pdf.annotations.GoToAction(document.pages[2])
document.outlines.append(second_bookmark)
document.page_mode = pdf.PageMode.USE_OUTLINES
document.save(output_pdf)
if __name__ == "__main__":
input_pdf = "input.pdf"
booklet_pdf = "booklet.pdf"
final_pdf = "booklet-with-bookmarks.pdf"
create_pdf_booklet(input_pdf, booklet_pdf)
add_bookmarks_to_booklet(booklet_pdf, final_pdf)
print("PDF booklet created successfully:", final_pdf)
Conclusion
In this tutorial, you learned how to create a PDF booklet in Python using Aspose.PDF for Python via .NET. You installed the SDK, created a booklet from an existing PDF, configured left and right page placement, added optional bookmarks, and saved the final booklet as a PDF file.
The PdfFileEditor.make_booklet() method is useful when you need to automate booklet generation for reports, catalogs, manuals, brochures, and printable documentation. By combining booklet creation with bookmarks, you can generate PDF files that are suitable for both print and digital distribution.
