Getting file not found and memory out of bound on running Powershell script through java for converting PPT, XLS to PPT

1

I am tiring to run powershell script through Java application.

I am tiring two operations

1) Converting ppt to pdf.

2) Converting xls to pdf.

Configurations I am using are,

MS Office 2013 on Windows 2008

Script for converting PPT to PDF is as follows

 #Convert Powerpoint formats to pdf
 Param(
   [string]$inputPath,
   [string]$outputPath
 )
 Add-type -AssemblyName Office
 Add-type -AssemblyName Microsoft.Office.Interop.PowerPoint
 $ppFormatPDF = 32

 $ppQualityStandard = 0
 $pp = new-object -comobject powerpoint.application

 $ppt =$pp.presentations.open($inputPath)
 $ppt.SavecopyAs($outputPath, $ppFormatPDF) # 32 is for PDF 
 $ppt.close()
 $pp.Quit()
 $pp = $null 
 [gc]::collect()
 [gc]::WaitForPendingFinalizers()

On running this script I am getting memory out of bound exception

 Executing command: powershell -NonInteractive -NoLogo -NoProfile -ExecutionPolicy UnRestricted -File D:\WEB-INF\classes\resource\pptToPdf.ps1 -inputPath D:\Temp\testPpt1.pptx -outputPath D:\ConvertedPdf1.pdf
  Command Successful: code=0, stderr=Exception calling "Open" with "1" argument(s): "Not enough storage is 
  available to complete this operation. (Exception from HRESULT: 0x8007000E 
  (E_OUTOFMEMORY))"
  At D:\WEB-INF\classes\resources\pptToPdf.ps1:15 char:1
  + $ppt =$pp.presentations.open($inputPath)
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
  + FullyQualifiedErrorId : ComMethodTargetInvocation

  You cannot call a method on a null-valued expression.
  At D:\WEB-INF\classes\resources\pptToPdf.ps1:16 char:1
  + $ppt.SavecopyAs($outputPath, $ppFormatPDF) # 32 is for PDF
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
  + FullyQualifiedErrorId : InvokeMethodOnNull

  You cannot call a method on a null-valued expression.
  At D:\WEB-INF\classes\resources\pptToPdf.ps1:17 char:1
  + $ppt.close()
  + ~~~~~~~~~~~~
  + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
  + FullyQualifiedErrorId : InvokeMethodOnNull

Can any one please guide me for what is going wrong ?

Second for XLS to PDF the script is

#Convert Excel formats to pdf

Param(
 [string]$inputPath,
 [string]$outputPath
)

$xlQualityStandard = 0 # Microsoft.Office.Interop.Excel.XlFixedFormatQuality

$xlFixedFormat = "Microsoft.Office.Interop.Excel.xlFixedFormatType" -as  [type] 

#$excelFiles = Get-ChildItem -Path $folderpath -include *.xls, *.xlsx -recurse
$wb= $inputPath;

$objExcel = New-Object -ComObject excel.application
$objExcel.visible = $false
$ci = [System.Globalization.CultureInfo]'en-US'
$workbook = $objExcel.workbooks.open($wb)
$workbook.Saved = $true

$Name = $outputPath 

$workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $Name)

$objExcel.Workbooks.close() 

$objExcel.Quit()

On running this script getting input file not found exception

 Executing command: powershell -NonInteractive -NoLogo -NoProfile -ExecutionPolicy UnRestricted -File D:\WEB-INF\classes\resources  \xlsToPdf.ps1 -inputPath D:\testXls.xls -outputPath D:\ConvertedPdf2.pdf
 Command Successful: code=0, stderr=Exception calling "Open" with "1"   argument(s): "Microsoft Excel cannot access 
 the file 
'D:\testXls.xls'. There 
are several possible reasons:
 The file name or path does not exist.
 The file is being used by another program.
 The workbook you are trying to save has the same name as a currently  open 
workbook."
At D:\WEB-INF\classes\resources\xlsToPdf.ps1:21 char:2
+  $workbook = $objExcel.workbooks.open($wb)
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation

Property 'Saved' cannot be found on this object; make sure it exists and is 
settable.
At D:\WEB-INF\classes\resources\xlsToPdf.ps1:22 char:2
+  $workbook.Saved = $true
+  ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound

You cannot call a method on a null-valued expression.
At D:\WEB-INF\classes\resources\xlsToPdf.ps1:26 char:2
+  $workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $Name)
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

I am running all these scripts through Java application. The code is as follows

 String command = "powershell -NonInteractive -NoLogo -NoProfile -ExecutionPolicy UnRestricted -File D:\\WEB-INF\\classes\\resources\xlsToPdf.ps1 -inputPath D:\\testXls.xls -outputPath D:\\ConvertedPdf2.pdf";
System.out.println(command);
// Executing the command
Process powerShellProcess = Runtime.getRuntime().exec(command);
// Getting the results
powerShellProcess.getOutputStream().close();
String line;
System.out.println("Standard Output:");
BufferedReader stdout = new BufferedReader(new InputStreamReader(
powerShellProcess.getInputStream()));
while ((line = stdout.readLine()) != null) {
System.out.println(line);
}
stdout.close();
System.out.println("Standard Error:");
BufferedReader stderr = new BufferedReader(new InputStreamReader(
powerShellProcess.getErrorStream()));
while ((line = stderr.readLine()) != null) {
System.out.println(line);
}
stderr.close();
System.out.println("Done");

The power shell commands are as follows

For XLS to PDF,

   powershell -NonInteractive -NoLogo -NoProfile -ExecutionPolicy   UnRestricted -File D:\WEB-INF\classes\resources\xlsToPdf.ps1 -inputPath D:\testXls.xls -outputPath D:\ConvertedPdf2.pdf

For PPT to PDF,

   powershell -NonInteractive -NoLogo -NoProfile -ExecutionPolicy UnRestricted -File D:\WEB-INF\classes\resources\pptToPdf.ps1 -inputPath D:\Temp\testPpt1.pptx -outputPath D:\ConvertedPdf1.pdf

I am getting these issues when I am tiring to run my script through my application. My application and tom cat are on D drive. When these where not working I made a standalone java class and run the scripts through that class, the scripts are working through that class. I have even executed the powershell commands through command prompt and powershell prompt also. They are working. Hence there is no issue with Windows 2008 and MS Office 2013.

I am missing something in my application. Can some one please guide me. Is there any permission issues or anything else ? This issue is troubling me for a long time.

Thankyou in advance.

java
excel
powershell
pdf
asked on Stack Overflow Mar 14, 2016 by User3091 • edited Mar 15, 2016 by User3091

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0