Convert Excel to PDF using JavaScript

0 votes

How can I convert Excel documents (files) to PDF in an automated fashion? I am trying to adapt the solution found here to Excel. So far I have this:

var fso = new ActiveXObject("Scripting.FileSystemObject");
var docPath = WScript.Arguments(0);
docPath = fso.GetAbsolutePathName(docPath);

var pdfPath = docPath.replace(/\.xls[^.]*$/, ".pdf");
var objExcel = null;

try
{
    WScript.Echo("Saving '" + docPath + "' as '" + pdfPath + "'...");

    objExcel = new ActiveXObject("Excel.Application");
    objExcel.Visible = false;

    var objExcel = objExcel.Workbooks.Open(docPath);

    var wdFormatPdf = 17;
    objExcel.SaveAs(pdfPath, wdFormatPdf);
    objExcel.Close();

    WScript.Echo("Done.");
}
finally
{
    if (objExcel != null)
    {
        objExcel.Quit();
    }
}

Output:

Saving 'somefile.xlsx' as 'somefile.pdf'...
Done.
..\SaveXLSXAsPDF.js(27, 9) (null): The object invoked has disconnected from its clients.

An erroneous PDF file is created, and it will not open in a reader. Perhaps format 17 isn't the best choice for Excel, I'm thinking. Does anyone have the correct information or know how to make this work?

Oct 9 in Others by Kithuzzz
• 20,660 points
369 views

1 answer to this question.

0 votes

You're clobbering objExcel on line 15:

var objExcel = objExcel.Workbooks.Open(docPath);

Those lines of code need to use a different variable, e.g.:

var objWorkbook = objExcel.Workbooks.Open(docPath);

var wdFormatPdf = 57;
objWorkbook.SaveAs(pdfPath, wdFormatPdf);
objWorkbook.Close();
answered Oct 9 by narikkadan
• 37,660 points

Related Questions In Others

0 votes
1 answer

How to convert pdf file to excel file using python

Just specify your whole output path instead ...READ MORE

answered Nov 4 in Others by narikkadan
• 37,660 points
218 views
0 votes
1 answer

How to convert excel to PDF using Python

Specify your whole output path instead of ...READ MORE

answered Nov 10 in Others by narikkadan
• 37,660 points
138 views
0 votes
1 answer

How to convert data from txt files to Excel files using python

Hi , there are few steps to ...READ MORE

answered Feb 16 in Others by Edureka
• 13,640 points
6,215 views
0 votes
0 answers

Convert Rows to Columns with values in Excel using custom format

1 I having a Excel sheet with 1 ...READ MORE

Feb 17 in Others by Edureka
• 13,640 points
112 views
0 votes
1 answer

Print chosen worksheets in excel files to pdf in python

In the simplest form: import win32com.client o = win32com.client.Dispatch("Excel.Application") o.Visible ...READ MORE

answered Sep 24 in Others by narikkadan
• 37,660 points
248 views
0 votes
1 answer

Java Spring - Writing Excel file and converting to PDF

Since you are using Spring I suggest ...READ MORE

answered Sep 26 in Others by narikkadan
• 37,660 points
272 views
0 votes
1 answer

Convert Excel to PDF issue with documents4j

MS Excel may not always be used ...READ MORE

answered Sep 26 in Others by narikkadan
• 37,660 points
198 views
0 votes
1 answer

Converting all tabs of excel sheet to PDF

Using VBA, try it like this, for ...READ MORE

answered Sep 26 in Others by narikkadan
• 37,660 points
232 views
0 votes
1 answer

Convert Excel and Word files to PDF Using ruby

 You can combine some: For excel files - ...READ MORE

answered Sep 26 in Others by narikkadan
• 37,660 points
145 views
0 votes
1 answer

How can I convert excel file to pdf using TCPDF?

PHPExcel can only read charts from Excel2007 ...READ MORE

answered Oct 21 in Others by narikkadan
• 37,660 points
145 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP