Join two or more PDFs into one in Business Central
In the next post we will explain how to join two PDFs into one, we will take as an example the generation of a Sales Order in PDF and Sales Invoice in the same format.
First we will create the codeunit as follows:
Then we will create a page where we will generate the pdf of the Sales Order and the Sales invoice:
page 50148 MergePDF{Caption = 'MergePDF';PageType = Card;layout{area(content){group(General){usercontrol(pdf; PDF){ApplicationArea = all;trigger DownloadPDF(pdfToNav: text)varTempBlob: Codeunit "Temp Blob";Convert64: Codeunit "Base64 Convert";Ins: InStream;Outs: OutStream;Filename: Text;beginif pdfToNav <> '' then beginFilename := 'Test.pdf';TempBlob.CreateInStream(Ins);TempBlob.CreateOutStream(Outs);Convert64.FromBase64(pdfToNav, Outs);DownloadFromStream(Ins, 'Download', '', '', Filename);end;end;}}}}actions{area(Processing){action(SalesOrder){ApplicationArea = All;Caption = 'Add Sales Order';Image = Order;trigger OnAction()varSalesHeader: Record "Sales Header";Recref1: RecordRef;begin//Get any recordSalesHeader.SetRange("Document Type", SalesHeader."Document Type"::Order);SalesHeader.FindFirst();SalesHeader.SetRange("No.", SalesHeader."No.");Recref1.GetTable(SalesHeader);MergePDF.AddReportToMerge(Report::"Standard Sales - Order Conf.", Recref1);Message(DocumentAdded, SalesHeader."No.");end;}action(SalesInvoice){ApplicationArea = All;Caption = 'Add Posted Sales Invoice';Image = Order;trigger OnAction()varSalesInvoiceHeader: Record "Sales Invoice Header";Recref2: RecordRef;beginSalesInvoiceHeader.FindFirst();SalesInvoiceHeader.SetRange("No.", SalesInvoiceHeader."No.");Recref2.GetTable(SalesInvoiceHeader);MergePDF.AddReportToMerge(Report::"Standard Sales - Invoice", Recref2);Message(DocumentAdded, SalesInvoiceHeader."No.");end;}action(Merge){ApplicationArea = All;Caption = 'Merge documents';Image = Print;trigger OnAction()begin//To check the JsonArray//Message(format(MergePDF.GetJArray()));CurrPage.pdf.MergePDF(format(MergePDF.GetJArray()));end;}action(Clear){ApplicationArea = All;Caption = 'Clear documents';Image = Delete;trigger OnAction()varbeginMergePDF.ClearPDF();end;}}}varMergePDF: Codeunit MergePDF;DocumentAdded: Label 'Document %1 added';}
Keep in mind that we must create controladdin for the PDF:
controladdin PDF
{
Scripts = 'src/script/pdf-lib.min.js',
'src/script/download.js',
'src/script/scripts.js';
MaximumHeight = 1;
MaximumWidth = 1;
event DownloadPDF(stringpdffinal: text);
procedure createPdf();
procedure MergePDF(JObjectToMerge: text);
}
Find the complete code here:
https://github.com/juancamilo0519/mergepdfbusinesscentral
Gonzalo Ríos Ley
https://www.linkedin.com/in/gonzalo-r%C3%ADos-ley/
Comentarios
Publicar un comentario