Creating a pdf file from the excel file added in the attachment

Amol Pawar
Tera Guru

Hi everyone,

 

I have a requirement where I need to create a pdf file from an attachment added in a record in ServiceNow suppose a request. When someone approves the request, the system will create a pdf file by taking some field values from an Excel sheet added to that request.

By using a script include it is doing as expected. But the problem is we need to create a table. Using CSS, we're able to create a table but the last column is going out of the page. I want to wrap up the text to next line below.

AmolPawar_0-1689934383092.png

 

Please help me to wrap up the text below. Is there anything I need to modify in CSS part of script include?

 

Thanks in advance!

Amol

 

3 REPLIES 3

Riya Verma
Kilo Sage
Kilo Sage

Hi @Amol Pawar ,

 

Hope you are doing great.

 

To ensure that the table within the PDF file fits within the page and does not encounter any formatting issues, we will adopt a different approach than using CSS alone.

 

  1.  Develop a script include to handle the generation of the PDF file. This script will extract relevant data from the Excel attachment and arrange it into a table format

  2. :Implement a business rule or workflow that triggers the script include upon request approval. This will ensure the PDF file generation occurs at the appropriate stage.

  3. Generate PDF with Correct Table Formatting:

    • Utilize a combination of HTML and CSS styles to create the table in the PDF. The formatting should be carefully structured to prevent any overflow issues. We can use libraries like PDFKit or jsPDF to assist with the PDF generation.

sample reference script:

 

function generatePDFFromExcelAttachment(requestId) {
  var excelAttachment = getExcelAttachment(requestId);
  var data = extractDataFromExcel(excelAttachment);
  
  var doc = new jsPDF();
  
  var header = ['Column 1', 'Column 2', 'Column 3', 'Column 4']; // Replace with actual column headers
  var rows = []; // Fill rows with data extracted from Excel
  
  // Set table header
  doc.setFont('helvetica', 'bold');
  doc.autoTable({
    head: [header],
    startY: 10,
    theme: 'grid'
  });
  
  // Set table data
  doc.autoTable({
    head: false,
    body: rows,
    startY: 20,
    theme: 'grid',
    columnStyles: {
      0: { columnWidth: 50 }, // Adjust column widths as needed
      1: { columnWidth: 50 },
      2: { columnWidth: 50 },
      3: { columnWidth: 50 }
    }
  });
  
  // Save the PDF
  doc.save('generated_pdf.pdf');
}

// Replace these functions with actual implementations to fetch attachment and extract data from the Excel sheet
function getExcelAttachment(requestId) {
  // Implement logic to retrieve the Excel attachment for the given request
}

function extractDataFromExcel(excelAttachment) {
  // Implement logic to parse the Excel attachment and extract necessary data
}

 

 
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

Amol Pawar
Tera Guru

Thank you @Riya Verma for your reply, I'll try to implement the solution you provided and let you know.

 

Regards,

Amol

Amol Pawar
Tera Guru

Hi @Riya Verma ,

 

generating pdf is done. I want to know while generating a pdf from an Excel sheet from an attachment, empty cells populating as "Null" in pdf. How can we import those cells as empty cells only, without adding "Null" keyword?

 

Thanks in advance if you could help me with this.

Regards,

Amol