List<String> files = ['input.pdf', 'invoice.pdf']; PdfDocument newDocument = PdfDocument(); PdfSection? section; for (String file in files) { // Load the PDF document. PdfDocument loadedDocument = PdfDocument(inputBytes: await _readData(file)); // Export the pages to the new document. for (int index = 0; index < loadedDocument.pages.count; index++) { // Get the page template. PdfTemplate template = loadedDocument.pages[index].createTemplate(); // Create a new section if the page settings are different. if (section == null || section.pageSettings.size != template.size) { section = newDocument.sections!.add(); section.pageSettings.size = template.size; section.pageSettings.margins.all = 0; } // Draw the page template to the new document. section.pages .add() .graphics .drawPdfTemplate(template, const Offset(0, 0)); } // Dispose the loaded document. loadedDocument.dispose(); } //Save the document. List<int> bytes = await newDocument.save(); //Disposes the document newDocument.dispose(); //Save the file and launch/download. SaveFile.saveAndLaunchFile(bytes, 'output.pdf'); |