Attempting to convert a doc to pdf without open of...
# cfml-general
j
Attempting to convert a doc to pdf without open office installed results in an untrappable error:
Could not initialize class com.sun.star.lib.loader.WinRegKey
I'd like to be able to attempt to convert and catch the error but that seems impossible-
Copy code
<cftry>
  <cfdocument format='pdf' srcfile='C:\ColdFusionWebroot\upload-testing\6personal_essay.docx' name='mydoc' />
  <cfcatch type='any'>
    <cfdump var='#cfcatch#'>
  </cfcatch>
</cftry>
a
I presume it's a compilation error. For the CF compiler to be able to do anything with
<cfdocument>
, it needs to be able to load the libs it needs to do so. Not sure this is the right place in the application's code to deal with this sort of thing.
j
If there a graceful way to know if openoffice is installed? I would have expected a trappable error to occur when calling cfdocument. Where in the code would you deal with this?
a
If there a graceful way to know if openoffice is installed?
Can't answer that. I doubt there is, but you could look for some top level class like org.openoffice.ThisIsTheMainThing, and write a wrapper around trying to instantiate one of those or something?
Copy code
isOpenOfficeInstalled() {
    try {
        createObject("java", "whatevs")
        return true
    } catch (any e) {
        return false
    }
}
(Obvs integration test that with it both installed and not installed ;-))
I would have expected a trappable error to occur when calling cfdocument.
I can understand how a plugin might be needed at compile time to even be able to compile the code. it's sloppy (for the reasons you allude to), but hey ho... this is ColdFusion, and "something being implemented sloppily" should not come as too much of a surprise. Also: I am just guessing it's a compile-time error. You didn't share the relevant bit of said error message.
Where in the code would you deal with this?
If OpenOffice ain't installed, shouldn't even be calling code that tries to use it. How does the logic flow get started to end up wanting to use
<cfdocument>
when the module needed to do so isn't installed? That's where I'd head things off. Let's say you have an "export to PDF" link in your UI, and that's where this all starts. Feature toggle the display of that based on whether you can actually do it or not. My rule of thumb is that "the line of code where the error is occurring" is seldom where the source of the issue is. If you start with that premise, and aim to solve the problem not treat the symptom of the problem, you'll likely end up with better designed code. (that said I have to guess all this cos there's only the one line of code to work with, and no real context. So... I'm guessing...)
j
Thanks for the reply. I was mostly being lazy here and hoping to just let the possible known error be trapped rather than have to write checks to see if OO is installed. I'm actually compiling multiple user uploaded documents into a pdf so the error trapping is there to catch random failures. I just never expected a hard crash to occur but it did on one of my local dev envs where OO is not installed.
👍 1