Hi guys!
I create a class in c#, to convert from "x" document to pDF.
The app works correctly in my local pc, but when migrate to the server I get this error:
System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an
exception.
at cppu.defaultBootstrap_InitialComponentContext(Reference<com::sun::star::uno::XComponentContext>*
)
at uno.util.Bootstrap.defaultBootstrap_InitialComponentContext(String ini_file, IDictionaryEnumerator
bootstrap_parameters)
----------------------------------------------------------------------------------
I use OpenOffice 3 and install OpenOffice.org_3.4.1_SDK.
this is my class:
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Web;
using log4net.Core;
using Microsoft.Win32;
using uno;
using uno.util;
using unoidl.com.sun.star.beans;
using unoidl.com.sun.star.bridge;
using unoidl.com.sun.star.connection;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.uno;
using WebSearch.Components.GeneralFunctions;
using WebSearch.Log;
using Exception = System.Exception;
namespace WebSearch.Components.CommonFunctions
{
public static class PDFUtility
{
public static void ConvertoDocumentToPdf(string fileName, string extension)
{
InitOpenOfficeEnvironment();
string path = HttpContext.Current.Server.MapPath("~/TempFolder/PDFs/");
LogConfig.SaveInFile(Level.Debug, "Get PDF path" + path, null,
typeof (File).ToString(),
"ConvertoDocumentToPdf");
string oldPath = HttpContext.Current.Server.MapPath("~/TempFolder/TempFiles/")
+ User.CurrentUserId() + @"\";
LogConfig.SaveInFile(Level.Debug, "Get Temp path" + oldPath, null,
typeof (File).ToString(),
"ConvertoDocumentToPdf");
if (System.IO.File.Exists(path + fileName)) System.IO.File.Delete(path + fileName);
if (System.IO.File.Exists(oldPath + fileName))
{
LogConfig.SaveInFile(Level.Debug, "File Exist" + oldPath + fileName, null,
typeof (File).ToString(),
"ConvertoDocumentToPdf");
try
{
bool value = ConvertToPdf(oldPath + fileName, path + fileName.ToLower().Replace(extension.ToLower(),
"pdf"));
if (value) System.IO.File.Delete(oldPath + fileName);
}
catch (Exception ex)
{
LogConfig.SaveInFile(Level.Error, "Convert PDF error", ex,
typeof (File).ToString(),
"ConvertoDocumentToPdf");
}
}
}
private static void StartOpenOffice()
{
Process[] ps = Process.GetProcessesByName("soffice.exe");
if (ps.Length != 0)
LogConfig.SaveInFile(Level.Error, "OpenOffice not found. Is OpenOffice installed?"
+ ps.Length, null,
typeof (File).ToString(),
"StartOpenOffice");
if (ps.Length > 0)
return;
var p = new Process
{
StartInfo =
{
Arguments = "-headless -nofirststartwizard",
FileName = "soffice.exe",
CreateNoWindow = true
}
};
LogConfig.SaveInFile(Level.Debug, "Create new Process", null,
typeof (File).ToString(),
"StartOpenOffice");
bool result = p.Start();
if (result)
LogConfig.SaveInFile(Level.Debug, "process Starts", null,
typeof (File).ToString(),
"StartOpenOffice");
if (result == false)
LogConfig.SaveInFile(Level.Error, "open Office start fails", null,
typeof (File).ToString(),
"StartOpenOffice");
}
private static string PathConverter(string file)
{
if (string.IsNullOrEmpty(file))
throw new NullReferenceException("Null or empty path passed to OpenOffice");
return String.Format("file:///{0}", file.Replace(@"\", "/"));
}
private static string ConvertExtensionToFilterType(string extension)
{
LogConfig.SaveInFile(Level.Debug, "Get Filter Type" + extension, null,
typeof (File).ToString(),
"ConvertExtensionToFilterType");
switch (extension)
{
case ".doc":
case ".docx":
case ".txt":
case ".rtf":
case ".html":
case ".htm":
case ".xml":
case ".odt":
case ".wps":
case ".wpd":
case ".jpg":
case ".png":
case ".bmp":
case ".gif":
return "writer_pdf_Export";
case ".xls":
case ".xlsb":
case ".xlsx":
case ".ods":
case ".csv":
return "calc_pdf_Export";
case ".ppt":
case ".pptx":
case ".odp":
return "impress_pdf_Export";
default:
return null;
}
}
private static void SaveDocument(XComponent xComponent, string sourceFile, string
destinationFile)
{
try
{
var propertyValues = new PropertyValue[2];
// Setting the flag for overwriting
propertyValues[1] = new PropertyValue {Name = "Overwrite", Value = new Any(true)};
//// Setting the filter name
propertyValues[0] = new PropertyValue
{
Name = "FilterName",
Value =
new Any(
ConvertExtensionToFilterType(Path.GetExtension(sourceFile.ToLower())))
};
((XStorable) xComponent).storeToURL(destinationFile, propertyValues);
}
catch (Exception ex)
{
LogConfig.SaveInFile(Level.Error, "Save error ", ex,
typeof (File).ToString(),
"SaveDocument");
}
}
private static bool ConvertToPdf(string inputFile, string outputFile)
{
if (ConvertExtensionToFilterType(Path.GetExtension(inputFile.ToLower())) == null)
LogConfig.SaveInFile(Level.Error, "Unknown file type for OpenOffice. File
= " + inputFile, null,
typeof (File).ToString(),
"ConvertoDocumentToPdf");
// StartOpenOffice();
bool value = false;
XComponent xComponent = null;
try
{
XComponentContext xContext = null;
RegistryKey regkey = Registry.LocalMachine.OpenSubKey(
@"SOFTWARE\OpenOffice.org\UNO\InstallPath", false);
if (regkey == null)
return false ;
var installPath = (string) regkey.GetValue("");
if (installPath == null)
return false;
string officePath = installPath + @"\soffice.exe";
string officeParams = "-headless -nologo -nofirststartwizard -terminate_after_init
-invisible";
string unoParams = "-accept='socket,host=localhost,port=8100;urp;StarOffice.Service'";
Environment.SetEnvironmentVariable(
"URE_BOOTSTRAP", "vnd.sun.star.pathname:" + installPath + "/fundamental.ini");
Process p = Process.Start(officePath, officeParams + unoParams);
XComponentContext xLocalContext = Bootstrap.defaultBootstrap_InitialComponentContext();
XMultiComponentFactory xLocalServiceManager = xLocalContext.getServiceManager();
var xUrlResolver = (XUnoUrlResolver) xLocalServiceManager.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", xLocalContext);
int i = 0;
while (i < 20)
{
try
{
xContext = (XComponentContext) xUrlResolver.resolve(
"uno:pipe,name=officepipe1;urp;StarOffice.ComponentContext");
if (xContext != null)
break;
}
catch (NoConnectException)
{
Thread.Sleep(100);
}
i++;
}
if (xContext == null)
return false;
var xMsf = (XMultiServiceFactory) xContext.getServiceManager();
Object desktop = xMsf.createInstance("com.sun.star.frame.Desktop");
var xLoader = (XComponentLoader) desktop;
xComponent = InitDocument(xLoader,
PathConverter(inputFile), "_blank");
//Wait for loading
while (xComponent == null)
{
Thread.Sleep(1000);
}
// save/export the document
SaveDocument(xComponent, inputFile, PathConverter(outputFile));
value = true;
}
catch (Exception ex)
{
LogConfig.SaveInFile(Level.Error, "Convert error" + ex.Message, ex,
typeof (File).ToString(),
"ConvertToPdf");
value = false;
}
finally
{
if (xComponent != null) xComponent.dispose();
}
return value;
}
private static XComponent InitDocument(XComponentLoader aLoader, string file, string
target)
{
var openProps = new PropertyValue[1];
openProps[0] = new PropertyValue {Name = "Hidden", Value = new Any(true)};
XComponent xComponent = aLoader.loadComponentFromURL(
file, target, 0,
openProps);
return xComponent;
}
[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsWow64Process([In] IntPtr hProcess, [Out] out bool lpSystemInfo);
public static bool Is64Bit()
{
bool retVal;
IsWow64Process(Process.GetCurrentProcess().Handle, out retVal);
return retVal;
}
public static string GetOSBit()
{
bool is64bit = Is64Bit();
if (is64bit)
return "x64";
else
return "x32";
}
}
----------------------------------------------------------------------
I tried diferents solutions, but nothing worked.
string officePath = installPath + @"\soffice.exe";
string officeParams = "-headless -nologo -nofirststartwizard -terminate_after_init
-invisible";
string unoParams = "-accept='pipe.....'";
-------------------------------------------------------------------------------
I forgot install some else in server? Such as Apache?
Please I need hel urgent
regards
--
|