Merge two pdf
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2025 02:33 AM
Hi Experts,
I have created two widgets in which I has placed all the html and css for the A4 size pdf
in my business rule i am calling the widget and html and css and creating the pdf using
two seperate pdf are getting created
now the requirement is to merge both the pdf to one pdf
i tried to concatenate but both the pdf are overlapping.
Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2025 02:52 AM
Hi @Appu
I don't think ServiceNow PDFGenerationAPI provide a direct way to merge PDFs, but you can use sn_pdfgeneratorutils.PDFGenerationAPI to generate multiple pages instead of creating separate PDFs.
Instead of concatenating the HTML and CSS for both widgets into one string (which causes overlapping), generate each widget separately and merge them into a single PDF with multiple pages.
Thanks,
Tushar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2025 03:25 PM - edited ‎02-27-2025 03:25 PM
HI Tushar,
Below is my Business rule in which I am calling the widgets twice and two pdf are generating
/////////////////////////////
// Create form PDF //
/////////////////////////////
var widget = new GlideRecord('sp_widget');
var html = '';
if (widget.get('2be8936887e34e10556c8a1a0cbb3504')) {
html = widget.getValue('template');
html += '<style>' + widget.getValue('css') + '</style>';
} else {
return;
}
var pdf = new sn_pdfgeneratorutils.PDFGenerationAPI;
if (current.age_group == 'Adult') {
pdf.convertToPDF(fieldMap(html, 'Adult'), current.getTableName(), current.getUniqueValue(), current.number + ' Unsigned Form');
} else if (current.age_group == 'Child') {
pdf.convertToPDF(
fieldMap(html, 'Child'), current.getTableName(), current.getUniqueValue(), current.number + ' Unsigned Form');
}
//////////////////////////////////////////
// Create information sheet PDF //
//////////////////////////////////////////
widget = new GlideRecord('sp_widget');
html = '';
if (widget.get('84329be6877b821058b932e80cbb35a3')) {
html = widget.getValue('template');
html += '<style>' + widget.getValue('css') + '</style>';
} else {
return;
}
if (current.age_group == 'Adult') {
pdf.convertToPDF(fieldMapPIS(html, 'Adult'), current.getTableName(), current.getUniqueValue(), current.number + ' Information Sheet');
} else if (current.age_group == 'Child') {
pdf.convertToPDF(fieldMapPIS(html, 'Child'), current.getTableName(), current.getUniqueValue(), current.number + ' Information Sheet');
}
The fieldMapPIS and fieldMap are the are the function to replace the values of the current form field values dynamically below is the function script.
function fieldMapPIS(html, audience) {
// Replace shared content
html = html.replace(
/{{c.data.short_description}}/g,
current.short_description
).replace(
/{{c.data.draft-watermark}}/g,
'display: none;'
).replace(
/{{c.data.patient.urn}}/g,
current.patient.urn
).replace(
/{{c.data.patient.family_name}}/g,
current.patient.surname
).replace(
/{{c.data.patient.given_names}}/g,
current.patient.given_name
).replace(
/{{c.data.patient.address}}/g,
current.patient.address
).replace(
/{{c.data.patient.dob}}/g,
current.patient.date_of_birth.getDisplayValue()
).replace(
/{{c.data.patient.gender_male}}/g,
current.patient.gender== 'M' ?
'<span class="checkbox-true"/>' :
'<span class="checkbox-false"/>'
).replace(
/{{c.data.patient.gender_female}}/g,
current.patient.gender == 'F' ?
'<span class="checkbox-true"/>' :
'<span class="checkbox-false"/>'
).replace(
/{{c.data.patient.gender_indeterminate}}/g,
current.patient.genger == 'I' ?
'<span class="checkbox-true"/>' :
'<span class="checkbox-false"/>'
).replace(
/{{c.data.statewide_code}}/g,
current.consent_template.statewide_code
).replace(
/{{c.data.version_number}}/g,
current.consent_template.version_number
).replace(
/{{c.data.content_review}}/g,
current.consent_template.content_review.getDisplayValue() == '' ? 'N/A' :
current.consent_template.content_review.getDisplayValue().split('/')[2]
).replace(
/{{c.data.check}}/g,
current.consent_template.check.getDisplayValue() == '' ? 'N/A' :
current.consent_template.check.getDisplayValue().split('/')[1] +
'/' +
current.consent_template.check.getDisplayValue().split('/')[2]
).replace(
/{{c.data.published}}/g,
current.consent_template.published.getDisplayValue() == '' ? 'N/A' :
current.consent_template.published.getDisplayValue().split('/')[1] +
'/' +
current.consent_template.published.getDisplayValue().split('/')[2].split(' ')[0]
).replace(
/{{c.data.pis_main}}/g,
current.patient_information_sheet
);
// Show adult sections and hide child sections
if (current.age_group == 'Adult') {
html = html.replace(
/{{c.data.display_adult}}/g,
''
).replace(
/{{c.data.display_child}}/g,
'display: none;'
);
// Show child sections and hide adult sections
} else if (current.age_group == 'Child') {
html = html.replace(
/{{c.data.display_adult}}/g,
'display: none;'
).replace(
/{{c.data.display_child}}/g,
''
);
// Show neither
} else {
html = html.replace(
/{{c.data.display_adult}}/g,
'display: none;'
).replace(
/{{c.data.display_child}}/g,
'display: none;'
);
}
html = fixRelativeLinks(html);
// Replace any interpolation that has no mapping above
html = html.replace(
/\{\{([^}]+)\}\}/g, // any string enclosed in {{ }}
'' // empty
);
return html;
}
Now How do I generate each widget separately and merge them into a single PDF with multiple pages.
I didnt get what you are trying to explain.
Your response is much appreciated.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-06-2025 11:02 PM
You can try a PDF library to merge them without overlapping content.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2025 05:21 AM
Thank you so much for the suggestion.