Hi guys, anyone here ever written a script to merg...
# suitescript
b
Hi guys, anyone here ever written a script to merge two pdf documents into one? I'm looking to do this on a User Event script 🤔
n
function mergePDFs(get_file) { try { if (get_file) { var xml = "<?xml version=\"1.0\"?>\n<!DOCTYPE pdf PUBLIC \"-//big.faceless.org//report\" \"report-1.1.dtd\">\n"; xml += "<pdfset>"; for (var s = 0; s < get_file.length; s++) { try { var fileURL = nlapiLoadFile(get_file[s]).getURL(); xml += "<pdf src='" + nlapiEscapeXML(fileURL) + "'/>"; } catch (er) { nlapiLogExecution('Debug', 'Error in getting individual files', er.message); } } xml += "</pdfset>"; var file = nlapiXMLToPDF(xml); return file; } else { nlapiLogExecution('Debug', 'No files to be merged.'); return false; } } catch (er) { nlapiLogExecution('Debug', 'Error in mergePDF function', er.message); return false; } }
💯 1
1000 1
This is SS-1.0. Had it's 2.0 equivalent as well but unable to find rn. You'll need to change a few API calls rest should work like magic I think.
Get File Parameter is an array of internal id(s) of files
b
@Nik The code keeps failing at
nlapiXMLToPDF( myxml )
The error i get is
Error Parsing XML: //system.eu2.netsuite.com/core/media/media.nl?id=5175759&c=3398896&h=b8264df6cdd72b6f2419&_xt=.pdf
n
You are using SS-1.0?
Share your code, and try to access the file you are trying to convert in here
b
@Nik Yes am using SS1.0, perhaps i should switch it to SS2.0??
function mergePDFs(get_file) { try { if (get_file) { var xml = "<?xml version=\"1.0\"?>\n<!DOCTYPE pdf PUBLIC \"-//big.faceless.org//report\" \"report-1.1.dtd\">\n"; xml += "<pdfset>"; for (var s = 0; s < get_file.length; s++) { try { var fileURL = nlapiLoadFile(get_file[s]).getURL(); xml += "<pdf src='" + nlapiEscapeXML(fileURL) + "'/>"; } catch (er) { nlapiLogExecution('Debug', 'Error in getting individual files', er.message); } } xml += "</pdfset>"; var file = nlapiXMLToPDF(xml); return file; } else { nlapiLogExecution('Debug', 'No files to be merged.'); return false; } } catch (er) { nlapiLogExecution('Debug', 'Error in mergePDF function', er.message); return false; } } var pdf_files = [5500134, 5477803] var myfile = mergePDFs(pdf_files);
n
Have you tried logging the generated file URL and if the role you are trying to do this with have access to those files. laos, the file size of the generated consolidated PDF is in the correct range?
Can you share the screenshot of the error. This very function when I ran with a couple of valid Files, ran perfectly for me.
b
@Nik You my friend, are a GENIUS!!!!!!!!!!!! Converted the script to 2.0 and it works!!! Wow! Lemme post my 2.0 version
🤓 1
n
Thanks! But I inherited this script and only modified based on my use. 😜 Go ahead. Post your 2.0 version. Want to know what resolved the error you received in first place. 🤔
b
function mergePDFs(get_file) { try { if (get_file) { var xmlDocument = "<?xml version=\"1.0\"?>\n"+ "<!DOCTYPE pdf PUBLIC \"-//big.faceless.org//report\" \"report-1.1.dtd\">\n"+ "<pdfset>"; for (var s = 0; s < get_file.length; s++) { try { var fileObj = file.load({'id': get_file[s] }); var fileURL = fileObj.url; var xmlEscapedDocument = xml.escape({ xmlText : fileURL }); xmlDocument += "<pdf src='" +xmlEscapedDocument+ "'/>"; } catch (er) { log.debug('Error in getting individual files'); } } xmlDocument += "</pdfset>"; var pdfFile = render.xmlToPdf({ xmlString: xmlDocument }); return pdfFile; } else { log.debug('No files to be merged'); return false; } } catch (er) { log.debug('Error in mergePDF function'); return false; } }
🔥 1
af 1
@Eric B The code above should work fine
function generateDocusignReadyFile(currentRecord, transaction_id, folder_id) { var supplierquote_id = currentRecord.getValue("custbody_external_order_attach"); var printTemplate_FileObj = render.transaction({ entityId: transaction_id, printMode: render.PrintMode.PDF }); printTemplate_FileObj.folder = folder_id; var final_filename = printTemplate_FileObj.name; printTemplate_FileObj.name = '_delete_'+printTemplate_FileObj.name var printTemplate_fileId = printTemplate_FileObj.save(); if(supplierquote_id && printTemplate_fileId && final_filename) { var supplierquote_FileObj = file.load({'id': supplierquote_id }); var post_printTemplate_FileObj = file.load({'id': printTemplate_fileId }); if(supplierquote_FileObj.fileType == 'PDF') { var xmlDocument = "<?xml version=\"1.0\"?>\n"+ "<!DOCTYPE pdf PUBLIC \"-//big.faceless.org//report\" \"report-1.1.dtd\">\n"+ "<pdfset>"; var supplierquote_fileURL = supplierquote_FileObj.url; var printTemplate_fileURL = post_printTemplate_FileObj.url; var xmlEscapedsupplierQuoteDocument = xml.escape({ xmlText : supplierquote_fileURL }); var xmlEscapedprintTemplateDocument = xml.escape({ xmlText : printTemplate_fileURL }); xmlDocument += "<pdf src='" +xmlEscapedprintTemplateDocument+ "'/>"; xmlDocument += "<pdf src='" +xmlEscapedsupplierQuoteDocument+ "'/>"; xmlDocument += "</pdfset>"; var pdfFile = render.xmlToPdf({ xmlString: xmlDocument }); pdfFile.folder = folder_id; pdfFile.name = final_filename; var merged_fileId = pdfFile.save(); if(merged_fileId){ file.delete({ id: printTemplate_fileId }); attachTemplateToRecord(merged_fileId, transaction_id); } } else { log.debug('Document merging did not happen coz supplier quote format is not of type PDF'); } } return pdfFile }
@Eric B The above is another implementation of the same, where i generate a pdf file for merging without using the available without login
e
@badazzke thanks!
b
Hi @Nik does this implementation have an exception? I've noticed some PDF documents get merged while some do not 🤔
n
Well, no one has ever reported any so far to me. And my logs are mostly clean. Can you share the exception message? Could be something due to some special symbols being used in PDF, in which case you may want to use some sort of cleaner/encoder function to parse the string to its Unicode equivalent. Something that will replace your '&' with '&amp;' and '<' with '&lt;' and all. I think you got the idea. Share the exception message and we'll see how to go about it.
b
@Nik
Copy code
{
  "type": "error.SuiteScriptError",
  "name": "UNEXPECTED_ERROR",
  "message": null,
  "stack": [
    "renderXmlToPdf(N/render)",
    "generateDocusignReadyFile(/SuiteScripts/WIOCC/Purchases/purchase_order_user_event.js:91)",
    "afterSubmit(/SuiteScripts/WIOCC/Purchases/purchase_order_user_event.js:166)"
  ],
  "cause": {
    "type": "internal error",
    "code": "UNEXPECTED_ERROR",
    "details": null,
    "userEvent": "aftersubmit",
    "stackTrace": [
      "renderXmlToPdf(N/render)",
      "generateDocusignReadyFile(/SuiteScripts/WIOCC/Purchases/purchase_order_user_event.js:91)",
      "afterSubmit(/SuiteScripts/WIOCC/Purchases/purchase_order_user_event.js:166)"
    ],
    "notifyOff": false
  },
  "id": "03f30caf-867d-4cd4-bfb2-89f5e82bec6d-2d323031392e30342e3134",
  "notifyOff": false,
  "userFacing": false
}
@Nik The function is as below function generateDocusignReadyFile(currentRecord, transaction_id, folder_id) { var subsidiary = currentRecord.getValue("subsidiary"); var supplierquote_id = currentRecord.getValue("custbody_external_order_attach"); if((subsidiary == 13) || (!supplierquote_id)) { supplierquote_id = currentRecord.getValue("custbody_quote_attachment"); } var printTemplate_FileObj = render.transaction({ entityId: transaction_id, printMode: render.PrintMode.PDF }); printTemplate_FileObj.folder = folder_id; var final_filename = printTemplate_FileObj.name; printTemplate_FileObj.name = '_delete_'+printTemplate_FileObj.name var printTemplate_fileId = printTemplate_FileObj.save(); if(supplierquote_id && printTemplate_fileId && final_filename) { var supplierquote_FileObj = file.load({'id': supplierquote_id }); if(!supplierquote_FileObj.isOnline) supplierquote_FileObj.isOnline = true; supplierquote_FileObj.save(); var post_printTemplate_FileObj = file.load({'id': printTemplate_fileId }); if(supplierquote_FileObj.fileType == 'PDF') { var xmlDocument = "<?xml version=\"1.0\"?>\n"+ "<!DOCTYPE pdf PUBLIC \"-//big.faceless.org//report\" \"report-1.1.dtd\">\n"+ "<pdfset>"; var printTemplate_fileURL = post_printTemplate_FileObj.url; var supplierquote_fileURL = supplierquote_FileObj.url; var xmlEscapedprintTemplateDocument = xml.escape({ xmlText : printTemplate_fileURL }); var xmlEscapedsupplierQuoteDocument = xml.escape({ xmlText : supplierquote_fileURL }); xmlDocument += "<pdf src='" +xmlEscapedprintTemplateDocument+ "'/>";// xmlDocument += "<pdf src='" +xmlEscapedsupplierQuoteDocument+ "'/>"; xmlDocument += "</pdfset>"; var pdfFile = render.xmlToPdf({ xmlString: xmlDocument }); pdfFile.folder = folder_id; pdfFile.name = final_filename; var merged_fileId = pdfFile.save(); if(merged_fileId){ file.delete({ id: printTemplate_fileId }); attachTemplateToRecord(merged_fileId, transaction_id); } } else { log.debug('Document merging did not happen coz supplier quote format is not of type PDF'); } } return pdfFile }
@Nik I have also sent you a copy of the file via direct message. Another thing I noticed is that the file has an adobe digital signature.. could that be the issue?