Here is an example - the following code is needed on all of our visualizations. We update it sometimes and need it to be the same everywhere. As this exists between the var template ’ - I have tried multiple ways of trying to pull it in from a central location - one place for everyone:
${eval(pm.environment.get(‘codePdf’))} - does not work. When I do that it returns an error in the script.
Our scripts have 3 - 8 components - the most common use case is 3.
- Lodash or handlebars to gather json data from the response. Handlebars within the template lodash outside using var
- Script libraries for either datatables or highcharts
- Script libraries for jsPDF (tried pdfmake but it has unsafe eval - which I can’t seem to find a way around - including setting meta tags in a header)
The below is our jsPdf script. Runs between after var template’ - used to allow users to pdf the reports we have developed. We would love to have this script in one location. I liked the variable idea - but it simple does not work. So we are copy pasting like little monkeys. Any thoughts on how to make the script below standard such tat we can call it from within the var template’ tags.
<!-- Creation of PDF generation from Postman visual -->
<script>
function generate() {
//Creation of PDF document
let doc = new jsPDF('l', 'pt');
const totalPagesExp = '{total_pages_count_string}';
var elem = document.getElementById('${pm.info.requestId}');
var data = doc.autoTableHtmlToJson(elem);
doc.autoTable(data.columns, data.rows, {
headStyles: {
cellWidth: 'wrap',
fontSize: 10,
lineWidth: 0.5,
lineColor: [0, 0, 0],
textColor: [0, 0, 0],
fillColor: [211,211,211]
},
bodyStyles: {
cellWidth: 'wrap',
fontSize: 8,
lineWidth: 0.5,
lineColor: [0, 0, 0],
textColor: [0, 0, 0],
fillColor: [255,255,255]
},
//Formatting of pages
didDrawPage: function (data) {
//Summa logo on top of the page
doc.addImage('${pm.variables.get("summa")}', 'PNG', 20, 20, 145, 42.63);
//Font sizes of report information
doc.setFontSize(8);
//Report information: portfolio name, knowledge time and report time
doc.text(35, 75, '${pm.variables.get("portfolioName")}');
doc.text(35, 85, '${pm.variables.get("reportTime")}');
doc.text(35, 95, '${pm.variables.get("knowledgeTime")}');
//Page numbers
var str = "Page " + doc.internal.getNumberOfPages()
if (typeof doc.putTotalPages === 'function') {
str = str + " of " + totalPagesExp;
};
//Page size
var pageSize = doc.internal.pageSize;
var pageHeight = pageSize.height ? pageSize.height : pageSize.getHeight();
doc.text(str, data.settings.margin.left, pageHeight - 10);
},
margin: {
top: 100
}
});
//Number of pages
if (typeof doc.putTotalPages === 'function') {
doc.putTotalPages(totalPagesExp);
}
//--------------------------------------------------------------------------------------------------START
//Change name of report if desired
doc.save('${pm.info.requestName}${pm.variables.get("reportTime")}.pdf');
//--------------------------------------------------------------------------------------------------END
}