Hi Guys, I’m trying to create a pdf file but getti...
# suitescript
n
Hi Guys, I’m trying to create a pdf file but getting error while file.create code: Kindly help here!!
Copy code
var xml = '<?xml version=\"1.0\"?>\n<!DOCTYPE pdf PUBLIC \"-//big.faceless.org//report\" \"report-1.1.dtd\">\n';
        xml += '<pdf>';
        xml += "<head>";
        xml += "<macrolist>";
        xml += "<macro id=\"myheader\">";
        xml += "<p align=\"center\" style='top:-35px' >";
        xml += "<b>Picking Ticket</b>";
        xml += "</p>"
        xml += "<p align=\"right\" style='top:-70px'>";
        xml += "Page <pagenumber/> of <totalpages/>";
        xml += "</p>";
        
        xml += "</macro>";
 
        xml += "<macro id=\"myfooter\">";
        
          xml += "   <p align='center' >";
          xml += "      <b>This is not an official document</b>";
          xml += "   </p>";
   
          xml += "<p align=\"right\" style='top:-25px;'>";
          //xml += "Page <pagenumber/> of <totalpages/>";
          xml += "</p>";
        xml += "</macro>";
        xml += "</macrolist>";
        xml += "</head>";
 
        xml += '<body style="font-family: sans-serif" header="myheader" header-height="20%" footer="myfooter" footer-height="10%" padding="0.5in 0.5in 0.5in 0.5in" size="Letter">';


//  Header of Item table
    xml += '<table page-break-after="always" style=" border: 0.3px solid black;top: -70px;font-size: 10pt;  width: 100%;">'
      xml += '  <thead>'
          xml += '   <tr>'
          xml += '    <td  style="border: 0.3px solid;font-weight:bold;border-right:0.3px solid;background-color: #E2EAF0;">SO</td>'
         // xml += '    <td  style="border: 0.3px solid;font-weight:bold;border-right:0;border-bottom:1;background-color: #E2EAF0;">SALES DEP.</td>'
          xml += '    <td  style="border: 0.3px solid;font-weight:bold;background-color: #E2EAF0;border-right:0.3px solid;">Subsidiary</td>'
          xml += '    <td  style="border: 0.3px solid;font-weight:bold;background-color: #E2EAF0;border-right:0.3px solid;">Item</td>'
          xml += '    <td  style="border: 0.3px solid;font-weight:bold;background-color: #E2EAF0;border-right:0.3px solid;">&nbsp;Description</td>'
          xml += '    <td  style="border: 0.3px solid;font-weight:bold;background-color: #E2EAF0;border-right:0.3px solid;">&nbsp;QTY </td>'
          xml += '    <td  style="border: 0.3px solid;font-weight:bold;background-color: #E2EAF0;border-right:0.3px solid;">&nbsp;Location</td>'
          xml += '    <td  style="border: 0.3px solid;font-weight:bold;background-color: #E2EAF0;border-right:0.3px solid;">&nbsp;Serial Number</td>'
          xml += '    <td  style="border: 0.3px solid;font-weight:bold;background-color: #E2EAF0;border-right:0.3px solid;">&nbsp;Bin Location</td>'
          xml += '    <td  style="border: 0.3px solid;font-weight:bold;background-color: #E2EAF0;border-right:0.3px solid;">&nbsp;Bin Qty</td>'
          //xml += '    <td  style="border: 0.3px solid;font-weight:bold;background-color: #E2EAF0;border-right:0.3px solid;">'+jsonObj.length+'</td>'
          // xml+='    <td  style="border: 0.3px solid;font-weight:bold;background-color: #E2EAF0;;border-bottom:1">&nbsp;Remarks</td>'
          xml += '   </tr>'
      xml += '  </thead>'


//
    
    for(var i = 0; i < jsonObj.length; i++){


       xml += '  <tr style ="border: 0.3px solid ; height:40; ">'
               xml += ' <td align="left" style ="border: 0.3px solid ;border-right:0.3px solid;">' + xmll.escape({
                   xmlText: jsonObj[i].soId
               }) + '</td>'
               
               xml += ' <td align="left" style ="border: 0.3px solid ;border-right:0.3px solid;"></td>'
               xml += ' <td align="left" style ="border: 0.3px solid ;border-right:0.3px solid;"></td>'
               xml += ' <td align="left" style ="border: 0.3px solid ;border-right:0.3px solid;"></td>'
               xml += ' <td align="left" style ="border: 0.3px solid ;border-right:0.3px solid;"></td>'
               xml += ' <td align="left" style ="border: 0.3px solid ;border-right:0.3px solid;"></td>'
               // xml+=' <td align="left" style ="border: 0.3px solid ;"></td>'
               //xml+=' <td align="left" style ="border: 0.3px solid ;">&nbsp;'+open_bal.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")+'</td>'
               xml += '  </tr>'



      
      log.debug('jsonObj length in loop!!',jsonObj.length);

    }

    xml += '  </table><br/>';


    xml += ''



       xml += '</body></pdf>'; // end PDF tags



      var fileObj = file.create({
        name: 'test.pdf',
        fileType: file.Type.PDF,
        contents: xml
        });

      fileObj.folder = 7954685;
      var fileId = fileObj.save();
b
N/file treats binary files like pdfs as base64 string
the string you are using as the contents is xml, it it not base64
n
Copy code
var base64EncodedString = encode.convert({
	          string:xml,
	          inputEncoding:encode.Encoding.UTF_8,
	          outputEncoding:encode.Encoding.BASE_64
	        });
b
nor is that how pdfs works, the xml you are trying to generate is used by N/render to generate a pdf file
n
i should do like this right??
b
N/render also uses freemarker as a template system so you dont actually need to try and build a string in code
n
can you give me link to any source?
I want to create a pdf with JSON data and email it using a schedule or MAP/reduce script
b
you will want to take a look at the N/render link first
usually you will require knowledge of Advanced PDF/HTML Templates to use it
n
okay
thanks
can’t I create it directly using N/xml module?
and file module to save file in file cabinate and then load it and use email module to email it
?
b
no, a pdf is not xml