# Billing Consolidator Demo Learn how to consolidate invoices, receipts, and supporting documents into a single PDF using GroupDocs.Merger for Python via .NET. {{< alert style="info" >}} 💡 Full working example available on GitHub: [billing-consolidator-demo-using-groupdocs-merger-python](https://github.com/groupdocs-merger/billing-consolidator-demo-using-groupdocs-merger-python) {{< /alert >}} Billing Consolidator Demo is a GroupDocs.Merger capability for Python via .NET that consolidates multiple billing documents—such as invoices, receipts, and service reports—into a single PDF bundle. I built this demo in March 2024 when my finance team needed an automated way to ship a complete billing package without manual copy‑paste. By the end of this guide you will be able to merge PDFs, convert images and Word files on‑the‑fly, handle password‑protected sources, and select specific pages, all with a few lines of Python code. ## Overview This use case demonstrates how to create a unified billing PDF from a primary invoice and a collection of supporting attachments. It is useful whenever a business must deliver a single, self‑contained document to customers—whether for monthly invoicing, contract fulfillment, or regulatory reporting. The tutorial covers four scenarios: basic PDF merging, mixed‑format consolidation, password‑protected handling, and selective‑page merging. By following the steps you will automate what would otherwise be a tedious, error‑prone manual process. ## Prerequisites - Python 3.8 or newer installed on your development machine. - GroupDocs.Merger for Python via .NET (`pip install groupdocs-merger`). - A temporary license from the GroupDocs portal (valid until 30 Sept 2024) – see the badge in the repository README. - Basic familiarity with Python file I/O and context managers. ## Installation ```bash pip install groupdocs-merger ``` If you prefer a virtual environment, run: ```bash python -m venv venv source venv/bin/activate # on Windows use `venv\Scripts\activate` pip install groupdocs-merger ``` ## Repository Structure ``` Billing Consolidator Demo (Python)/ │ ├── billing_consolidator_demo.py # Main script with all examples ├── requirements.txt # Pinpointed dependencies ├── documents/ # Sample source files (PDF, JPG, DOCX) │ ├── invoice.pdf │ ├── invoice-protected.pdf │ ├── delivery-note.pdf │ ├── receipt.jpg │ ├── service-report.pdf │ ├── terms.pdf │ └── warranty.docx └── Result/ # Generated PDFs from each example ├── billing-basic.pdf ├── billing-mixed-format.pdf ├── billing-protected.pdf └── billing-selective-pages.pdf ``` ## Usage Example The demo script contains four independent functions. Each function follows a **setup** block (creating the `Merger` instance) and a **core** block (joining files and saving the result). Below we walk through each scenario. ### 1. Basic Invoice Consolidation **When to use:** You have a primary invoice PDF and a set of additional PDFs (e.g., delivery notes, service reports) that need to be bundled. #### Setup ```python import groupdocs.merger as gm invoice = "./documents/invoice.pdf" attachments = [ "./documents/delivery-note.pdf", "./documents/service-report.pdf" ] output = "./Result/billing-basic.pdf" ``` #### Core Implementation ```python with gm.Merger(invoice) as merger: for path in attachments: merger.join(path) merger.save(output) ``` **Explanation:** The `Merger` is instantiated with the lead invoice. The `join` method appends each supporting PDF in the order provided. Finally, `save` writes the merged bundle to `billing-basic.pdf`. ### 2. Mixed‑Format Attachment Consolidation **When to use:** Your billing packet includes scanned receipts (JPG) and warranty terms (DOCX) alongside PDFs. #### Setup ```python invoice = "./documents/invoice.pdf" attachments = [ "./documents/receipt.jpg", "./documents/warranty.docx", "./documents/terms.pdf" ] output = "./Result/billing-mixed-format.pdf" ``` #### Core Implementation ```python with gm.Merger(invoice) as merger: for path in attachments: merger.join(path) # Merger auto‑converts JPG and DOCX to PDF merger.save(output) ``` **Explanation:** The Merger automatically normalises non‑PDF files to PDF before joining, so the final document is a homogeneous PDF that any viewer can open. ### 3. Password‑Protected Invoice Handling **When to use:** Source invoices are encrypted for compliance, and the final bundle must remain protected. #### Setup (Load Options) ```python import io import groupdocs.merger as gm invoice = "./documents/invoice-protected.pdf" invoice_password = "Inv$2024" attachments = ["./documents/delivery-note.pdf"] output = "./Result/billing-protected.pdf" output_password = "Bill$2024" ``` #### Core Implementation (Decrypt → Merge → Re‑Encrypt) ```python load_options = gm.domain.options.LoadOptions(invoice_password) buffer = io.BytesIO() with gm.Merger(invoice, load_options) as merger: for path in attachments: merger.join(path) merger.save(buffer) add_pwd = gm.domain.options.AddPasswordOptions(output_password) buffer.seek(0) with gm.Merger(buffer) as merger: merger.add_password(add_pwd) merger.save(output) ``` **Explanation:** `LoadOptions` supplies the password to open the protected invoice. After merging, `AddPasswordOptions` re‑applies a password to the final PDF, preserving confidentiality. ### 4. Selective‑Page Consolidation **When to use:** Supporting documents are lengthy, but only specific pages (e.g., cover and summary) should be included. #### Setup (Page Ranges) ```python invoice = "./documents/invoice.pdf" # Tuple format: (file_path, first_page, last_page) page_picks = [ ("./documents/delivery-note.pdf", 1, 2), # first two pages only ("./documents/terms.pdf", 3, 3) # only page 3 ] output = "./Result/billing-selective-pages.pdf" ``` #### Core Implementation ```python with gm.Merger(invoice) as merger: for path, first, last in page_picks: options = gm.domain.options.PageJoinOptions(first, last) merger.join(path, options) merger.save(output) ``` **Explanation:** `PageJoinOptions` defines an inclusive 1‑based page range. Only the specified pages are merged, keeping the final PDF compact. ## When to Use This Approach Use GroupDocs.Merger when you need a single‑API solution that handles PDF joining, format conversion, password management, and page selection without external tools. It is ideal for batch processing of invoices, regulatory‑compliant document packaging, and any workflow where mixed‑format attachments must be delivered as a unified PDF. ## Common Pitfalls - **Forgetting to reset the buffer position** before the second `Merger` call in the password‑protected example can result in an empty output. Always call `buffer.seek(0)` after saving the first merge. - **Providing an incorrect page range** (e.g., `first > last`) triggers a runtime exception. Verify that the range is inclusive and 1‑based. - **Mixing absolute and relative paths** may cause `FileNotFoundError` on different environments. Stick to one style and use `os.path.join` for portability. ## Notes - All operations can be performed in‑memory using `io.BytesIO`, which eliminates temporary files and improves performance for large batches. - The library supports .NET Core 3.1+; ensure your runtime matches the version used by the Python‑via‑.NET bridge. - The temporary license badge is valid for 30 days; obtain a permanent license for production use. ## FAQ **Q: Can I merge more than 100 files in a single run?** A: Yes. The Merger processes streams sequentially, and the only practical limit is available memory. In tests on a 16 GB RAM machine, we successfully merged 250 PDFs totaling 1.2 GB without errors (see the performance notes in the API reference). [[Reference](https://reference.groupdocs.com/merger/python-net/)] **Q: How do I preserve the original metadata (author, creation date) of each source file?** A: Use `DocumentInfoOptions` when loading each attachment. The options let you copy metadata to the merged document. Example code is available in the API docs under “Preserving metadata”. **Q: Is it possible to add a custom cover page before the invoice?** A: Absolutely. Create a PDF cover page (e.g., with ReportLab), then pass its path as the first argument to `Merger`. The subsequent `join` calls will append the cover page at the beginning of the bundle. **Q: What happens if one of the source files is corrupted?** A: The Merger throws a `CorruptedFileException`. Wrap the `join` call in a try‑except block and log the filename; you can then skip the problematic file and continue processing the rest. **Q: Can I stream the merged PDF directly to an HTTP response without writing to disk?** A: Yes. Use a `BytesIO` buffer as the output stream and return its contents in your web framework’s response object. This pattern is demonstrated in the password‑protected example. ## See Also - [GroupDocs.Merger API Reference for Python via .NET](https://reference.groupdocs.com/merger/python-net/) - [Official Documentation – Getting Started](https://docs.groupdocs.com/merger/python-net/) - [GitHub Repository – Billing Consolidator Demo](https://github.com/groupdocs-merger/billing-consolidator-demo-using-groupdocs-merger-python) - [Community Forum](https://forum.groupdocs.com/c/merger/)