Document Merge API
Generate combined PDF documents by merging multiple files and system-generated reports. Uses Playwright for HTML-to-PDF conversion and pypdf for PDF merging.Merge Project Documents
Path Parameters
Project ID
Query Parameters
Filter which documents to include. Options:
all(default) - All project documentscustody_chains- Only custody chain documentssheet_files- Only work sheet filesshipping_guides- Only shipping guide files
Filter to documents from a specific work sheet (optional)
Response
Returns a PDF file for download withContent-Type: application/pdf.
Filename format: Documentos_{partner_name}_{scope}.pdf
Document Order
-
Sheet Files (if included):
- Sheet project file
- Certificate of final disposition
- Invoice file
-
Custody Chains (if included):
- Ordered by activity date and consecutive number
-
Shipping Guides (if included):
- Ordered by issue date
Error Response
Always false on error
Error message
Merge Work Sheet Documents
Path Parameters
Work sheet ID
Response
Returns a merged PDF file for download. Filename format:Merge_{series_code}_{partner_name}.pdf
Document Composition
Documents are merged in this exact order:-
Work Sheet Report (system-generated, landscape)
- Quantities and materials worksheet
- Generated from
/workorders/worksheet/{id}/template
-
Final Disposition Certificate (system-generated, portrait)
- Certificate of final disposition
- Generated from
/workorders/final-disposition-certificate/{id}/template
-
Invoice File (uploaded)
- Sales invoice PDF (if attached)
-
Custody Chain Documents (system-generated)
- All custody chains for this sheet
- Ordered by activity date and consecutive number
- Generated from
/workorders/custody-chain-report/{id}/template
Technical Implementation
- Uses Playwright with Chromium to render HTML templates as PDFs
- Renders in headless mode with proper wait for network idle
- Copies authentication cookies from the request for authorized access
- Runs in a separate thread to avoid async event loop conflicts
- Timeout: 120 seconds
Merge Vehicle Documents
Path Parameters
Vehicle ID
Response
Returns a merged PDF file for download. Filename format:Documentos-Vehiculo-{plate}-{YYYYMMDD}.pdf
Document Composition
-
Vehicle Status Report (system-generated, portrait)
- Generated from
/vehicles/{id}/status-report/template
- Generated from
-
Insurance Policy (uploaded)
- Póliza de Seguro PDF
-
Vehicle Registration (uploaded)
- Matrícula PDF
-
Technical Inspection (uploaded)
- Revisión Técnica PDF
-
Technical Certifications (uploaded)
- All certification documents
- Ordered by certification name
-
Access Passes (uploaded)
- All block/facility passes
- Ordered by block name
Merge Technical Staff Documents
Path Parameters
Technical staff ID
Response
Returns a merged PDF file for download. Filename format:Documentos-Tecnico-{dni}-{name}-{YYYYMMDD}.pdf
Document Composition
-
Technical Information Report (system-generated, portrait)
- General technical profile and certifications
- Generated from
/technicals/{id}/information-report/template
-
Vaccination Report (system-generated, portrait)
- Complete vaccination history
- Generated from
/technicals/{id}/vaccine-report/template
-
National ID (uploaded)
- Cédula PDF
-
Driver’s License (uploaded)
- License PDF
-
Vaccination Certificate (uploaded)
- General vaccination certificate PDF
-
Technical Passes (uploaded)
- All block/facility access passes
- Ordered by block name
-
Individual Vaccination Records (uploaded)
- Specific vaccine administration records
- Ordered by application date
PDF Merging Process
System-Generated PDFs
-
HTML Rendering
- Django templates are rendered with context data
- Playwright navigates to the template URL
- Waits for
networkidlestate (all resources loaded) - Generates PDF with specified format and margins
-
Default Settings
- Format: A4
- Orientation: Portrait (landscape for worksheets)
- Margins: 0.5cm (1cm for certificates)
- Background printing: Enabled
File Handling
-
Uploaded PDFs
- Read directly from file storage
- Validated before inclusion
- Corrupted files are skipped with metadata note
-
Merge Order
- Documents are added to
PdfWriterin specification order - Each page from each document is appended sequentially
- Documents are added to
-
Error Handling
- Individual document failures don’t halt the entire process
- Failed documents are logged in PDF metadata
- Process continues with remaining documents
Authentication
All merge operations preserve user authentication:- Cookies from the incoming request are extracted
- Cookies are passed to Playwright browser context
- Ensures system-generated reports respect user permissions
Performance Considerations
- Threading: Playwright runs in a dedicated thread to avoid event loop conflicts
- Timeout: 120-second maximum per merge operation
- Memory: Large PDFs may require significant memory during merge
- Caching: No caching - documents are generated fresh on each request
Use Cases
Project Audit Package
Generate a complete audit package for a project:Quality Control Review
Generate custody chains only for review:Staff Compliance Package
Generate complete documentation for field deployment:Vehicle Fleet Audit
Generate documentation for all vehicles:Error Handling
Common Errors
404 Not Found- Operations that exceed 120 seconds will fail
- Consider reducing the scope or splitting into multiple requests
- Individual corrupted files are skipped
- Metadata in resulting PDF notes skipped files
- Operation continues with remaining documents