0

I have the following powershell script to open excel file and use excel save as PDF feature to generate a PDF file, and it works like a charm.

D:\test.ps1

$xlFixedFormat = "Microsoft.Office.Interop.Excel.xlFixedFormatType" -as [type] 
$xlRAMF = "Microsoft.Office.Interop.Excel.XlRunAutoMacro" -as [type] 
$xl = New-Object -comobject Excel.Application
# Show Excel
$xl.visible = $true
$xl.DisplayAlerts = $False
# Create a workbook
$wb = $xl.Workbooks.open("C:\Users\Administrator\Desktop\CAP\Book1.xlsx") 
$wb.RunAutoMacros($xlRAMF::xlAutoActivate)
$wb.Saved = $true

$wb.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, "C:\Users\Administrator\Desktop\CAP\Book1.pdf")
$xl.Workbooks.close() 
$xl.Quit()

And then we wrote this test JSP script to call that powershell script.

<%@page import="org.apache.cactus.server.*" session="true" %>
<%
    Runtime runtime = Runtime.getRuntime();
        Process proc = runtime.exec("powershell D:\\test.ps1");
        java.io.InputStream is = proc.getInputStream();
        java.io.InputStreamReader isr = new java.io.InputStreamReader(is);
        java.io.BufferedReader reader = new java.io.BufferedReader(isr);
        String line;
        while ((line = reader.readLine()) != null)
        {
            out.println(line);
        }
        reader.close();
        proc.getOutputStream().close();
%>

When running this JSP script the Excel application is started under local administrator, which is right because I already configured tomcat to run as a service using local administrator, but it never generates the PDF file nor the excel application is closed, could it be a security issue ?, for example do I need to change catalena security policy file ?, etc. I already did the steps found here

Community
  • 1
  • 1
QuakeCore
  • 1,886
  • 2
  • 15
  • 33
  • Is there anything in the question that is unclear in anyway ? – QuakeCore Oct 08 '15 at 12:02
  • Assuming that this is possible I wonder if there is just initial configuration that needs to be done. Have you run Excel while logged in as said administrator? In case Office has some things to set in the registry like owner information or something. – Matt Oct 08 '15 at 12:04
  • @matt yes I have, without any problems. – QuakeCore Oct 08 '15 at 12:07

0 Answers0