Create PDF Booklet
Creating booklet-style documents is a common requirement when preparing PDFs for printing. In a booklet layout, pages are rearranged so that when printed and folded, they appear in the correct order.
Using Aspose.PDF for Python, developers can easily convert a standard PDF into a booklet using the PdfFileEditor class. The ‘make_booklet’ method automatically reorganizes the pages of the input document and generates a new PDF optimized for booklet printing.
- Open an existing PDF document.
- Create a PdfFileEditor instance.
- Use the make_booklet method to reorganize the pages.
- Save the output as a booklet-ready PDF file.
import aspose.pdf as ap
import aspose.pdf.facades as pdf_facades
from io import FileIO
import sys
from os import path
sys.path.append(path.join(path.dirname(__file__), ".."))
from config import set_license, initialize_data_dir
# Create PDF Booklet
def create_pdf_booklet(infile, outfile):
# Create BookletMaker object
booklet_maker = pdf_facades.PdfFileEditor()
# Make booklet from input PDF file and save to output PDF file
booklet_maker.make_booklet(FileIO(infile), FileIO(outfile, "w"))
This code snippet shows how to use the ’try_make_booklet’ method of the PdfFileEditor class to rearrange pages for booklet printing without throwing exceptions if the operation fails.
A booklet layout rearranges pages so that when printed and folded, the document reads in the correct order. Automating this process ensures consistent results and eliminates the need for manual page rearrangement.
The ’try_make_booklet’ method works similarly to ‘make_booklet’, but with an important difference:
- ‘make_booklet’ throws an exception if the operation fails.
- ’try_make_booklet’ returns True or False, allowing developers to manage errors more safely.
- Open an existing PDF document.
- Create a PdfFileEditor instance.
- Attempt to Create the Booklet.
- Handle the Result.
import aspose.pdf as ap
import aspose.pdf.facades as pdf_facades
from io import FileIO
import sys
from os import path
sys.path.append(path.join(path.dirname(__file__), ".."))
from config import set_license, initialize_data_dir
def try_create_pdf_booklet(infile, outfile):
# Create BookletMaker object
booklet_maker = pdf_facades.PdfFileEditor()
# Make booklet from input PDF file and save to output PDF file
# The try_make_booklet method is like the make_booklet method,
# except the try_make_booklet method does not throw an exception if the operation fails.
if not booklet_maker.try_make_booklet(FileIO(infile), FileIO(outfile, "w")):
print("Failed to create booklet.")