Merge Multiple PDF

dharani2607
Tera Contributor

How to Merge multiple PDF's and the merged pdf needs to be visible in the request form in attachment section. 

5 REPLIES 5

Ravi Gaurav
Giga Sage
Giga Sage

You can merge multiple PDF files and attach the merged PDF file to a ServiceNow request form using a combination of ServiceNow's document API and workflow activities. Here are the general steps:

Create a workflow with a script activity to merge the PDF files. You can use a third-party library such as iText or PDFBox to perform the merging. Here is some example code using the iText library:

// Create a new document
Document document = new Document();

// Create a PdfCopy object to merge the files
PdfCopy copy = new PdfCopy(document, new FileOutputStream("merged.pdf"));
document.open();

// Loop through the files to be merged
String[] fileNames = {"file1.pdf", "file2.pdf", "file3.pdf"};
for (String fileName : fileNames) {
// Open the input PDF
PdfReader reader = new PdfReader(fileName);
// Merge the input PDF into the output document
copy.addDocument(reader);
// Close the input PDF
reader.close();
}

// Close the output document
document.close();

Use a document API call to upload the merged PDF file to the ServiceNow instance. You can use the sn_doc_api global object to make the API call. Here is some example code:

// Set up the API parameters
var file = new GlideSysAttachment();
file.write('merged.pdf', 'application/pdf', new Packages.java.io.File('merged.pdf'));

var gr = new GlideRecord('sys_attachment');
gr.initialize();
gr.table_name = 'sc_request';
gr.table_sys_id = current.sys_id;
gr.file_name = 'merged.pdf';
gr.content_type = 'application/pdf';
gr.size_bytes = file.getSize();
gr.sys_created_by = gs.getUserID();
gr.sys_created_on = gs.nowDateTime();

// Insert the attachment record and upload the file
var attachmentSysId = gr.insert();
file.write(gr.sys_id);

Set the attachment field on the request form to the attachment record created in step 2. You can use a script to set the value of the field to the attachment record sys_id. Here is some example code:

// Set the value of the attachment field to the attachment record sys_id
var attachmentSysId = gr.sys_id;
current.variables.attachment = 'sys_attachment:' + attachmentSysId;


If my answer solved your issue, please mark my answer as ‌‌ Correct & ‌‌Helpful based on the Impact.

 

Thanks

Ravi Gaurav

 

 

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

All the above scripts should be written in workflow run script? 

Aly
Tera Contributor

Hello @dharani2607 

Did you manage to merge PDFs in Servicenow ?

I have a similar requirement. It would be of great help if you have a solution.

Thanks,
Aly

Sanjay Sharma
Tera Expert

Hello Dharani
We have similar requirments where we need to merge multiple PDF attchments into a single PDF.
Did you manage to implement merge PDF?
Any lead or advice will really help us.
Thanks!
Sanjay