pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From shawn.oplinger@gmail.com <shawn.oplin...@gmail.com>
Subject Re: How can a java class load a static pdf file in WebLogic 12c?
Date Thu, 19 Apr 2018 17:05:07 GMT


On 2018/04/19 14:41:18, Christopher Schultz <chris@christopherschultz.net> wrote: 
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA256
> 
> Fabio,
> 
> On 4/19/18 10:26 AM, Fabio Salvi wrote:
> > Hallo Shawn
> > 
> > I use something like this:
> > 
> > InputStream resourceAsStream = 
> > getClass().getClassLoader().getResourceAsStream("/META-INF/pdfforms/"
> > + *aPDFFormularName*);
> > 
> > PDDocument pdfDocument = PDDocument.*load*(resourceAsStream);
> > 
> > This inside an EJB but I believe it will work for a WAR as well
> 
> Yes, it will work inside of anything where the ClassLoader can get to
> the file. Sometimes it makes more sense to use the servlet's
> getResource() method because it may have a wider selection of storage
> locations to consult.
> 
> A few things:
> 
> 1. Make sure to close the InputStream in a finally block. Resource
> leaks are serious business on servers like this.
> 
> 2. Watch out for your memory settings. You may end up loading a *huge*
> PDF into memory without realizing it.
> 
> 3. Remember that ClassLoader.getResourceAsStream can return null.
> 
> - -chris
> 
> > 2018-04-19 15:13 GMT+02:00 shawn.oplinger@gmail.com < 
> > shawn.oplinger@gmail.com>:
> > 
> >> 
> >> I need to load a static PDF document, from a java class, running
> >> in my J2EE web app on WebLogic 12c; however although my code
> >> works in Tomcat, when trying to run it in WebLogic 12c (WebLogic
> >> Server Version: 12.2.1.2.0), I get a server error that the PDF
> >> file cannot be found ( java.io.FileNotFoundException).
> >> 
> >> I am using Apache's PDF library, PDFBox version 2.0.8 to load a
> >> fillable PDF file that I created, and then populate that fillable
> >> PDF with data.  My code works fine in Tomcat, but fails to find
> >> the pdf file when deployed to WebLogic 12c .
> >> 
> >> -This appears to be because when an EAR file is deployed to
> >> WebLogic 12c, the contents in the WAR file (all of the
> >> application code/files, including the fillable PDF file), remain
> >> archived up in a jar file that WebLogic creates, instead of
> >> exploded.
> >> 
> >> My application utilizes the standard Maven application structure,
> >> so as is standard with all static files, I have put my PDF file
> >> in the directory for static resources: src/main/resources/
> >> 
> >> In my pom.xml file, I have the following, which builds any pdf
> >> files in the /src/main/resources/ folder, into the class path
> >> root folder of the WAR file. <resource> 
> >> <directory>${basedir}/src/main/resources/</directory> <includes>

> >> <include>**/*.xml</include> <include>**/*.properties</include>

> >> <include>**/*.pdf</include> </includes> </resource>
> >> 
> >> When I build the WAR and EAR file, the pdf file does indeed get
> >> copied into the root folder of the application's class files.
> >> 
> >> The following 3 lines of code, work to load the PDF, when my
> >> application's EAR file is deployed in Tomcat, but do not in
> >> WebLogic 12c (WebLogic Server Version: 12.2.1.2.0).
> >> 
> >> //this classLoader works for Tomcat, but no in WebLogic 12c 
> >> ClassLoader classLoader = getClass().getClassLoader(); File file=
> >> new File(classLoader.getResource(" myPdfFile.pdf").getFile()); 
> >> PDDocument document = PDDocument.load(file);
> >> 
> >> WebLogic 12c produces the following error:
> >> 
> >> ####<Apr 17, 2018, 1:21:50,798 PM EDT> <Error> <HTTP>
> >> <HQCIOLT1667133> <DefaultServer> <[ACTIVE] ExecuteThread: '6'
for
> >> queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>>
> >> <> <3902331f-a214-42fe-a6a1-35b3531e4b56-000000a9>
> >> <1523985710798> <[severity-value: 8] [rid: 0] [partition-id: 0]
> >> [partition-name: DOMAIN] > <BEA-101019>
> >> <[ServletContext@1661988196[app:mmhsrp-ear-4.9.0.1-3 
> >> module:/mmhsrp path:null spec-version:3.1]] Servlet failed with
> >> an IOException. java.io.FileNotFoundException:
> >> C:\Users\shawn.oplinger\ 
> >> AppData\Roaming\JDeveloper\system12.2.1.2.42.161008.1648\ 
> >> DefaultDomain\servers\DefaultServer\tmp\_WL_user\ 
> >> mmhsrp-ear-4.9.0.1-3\l27tj7\war\WEB-INF\lib\_wl_cls_gen.jar!\myPdfFil
> e.pdf
> >>
> >> 
> (The system cannot find the path specified)
> >> 
> >> 
> >> -When I browse my file system, to manually try to find the pdf
> >> file using the path in the WebLogic I can get as far as this
> >> directory: 
> >> C:\Users\shawn.smith\AppData\Roaming\JDeveloper\system12.2. 
> >> 1.2.42.161008.1648\DefaultDomain\servers\DefaultServer\tmp\_WL_user\
> >>
> >> 
> mmhsrp-ear-4.9.0.1-3\l27tj7\war\WEB-INF\lib\
> >> 
> >> in that directory is then this file: _wl_cls_gen.jar
> >> 
> >> in that jar file is indeed my pdf file:myPdfFile.pdf
> >> 
> >> -So why can't the classLoader find/load the pdf file? -does it
> >> have anything to do with the pdf file is actually in the
> >> archived _wl_cls_gen.jar file, and not exploded?
> >> 
> >> -Any suggestions on how my java class can load a static pdf file
> >> in WebLogic 12c?
> >> 
> >> Thanks! Shawn
> >> 
> >> ---------------------------------------------------------------------
> >>
> >> 
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> >> For additional commands, e-mail: users-help@pdfbox.apache.org
> >> 
> >> 
> > 
> -----BEGIN PGP SIGNATURE-----
> Comment: GPGTools - http://gpgtools.org
> Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
> 
> iQIzBAEBCAAdFiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAlrYqo4ACgkQHPApP6U8
> pFgoEBAAqHeC8FCbeRrfYCDlxTq0lEKhU8HNUv3Jzi9jveoNssUiuCM21VgLjsg8
> iIR8DXvQWGoo2jIuDqnio/7SWtn7YTPdXHcgwICdwnBrGxPO5TAevBWs5fxYvJsq
> R7iT+tW6aJdSUEBne/TAkGmEqnWZlSm+eitFD5KDvmNjATKw3TJMM1qgI/YVnjND
> oTBmzOw/qh1dmpg4TM94z4+Z2Z2UaBb0cfj+6J98QiEY1fJdEAAPeA09edRR22On
> 85kfG0kHtmW8cxWYJTa4AOfj0Vg9UKGdQjlP8E9k7V00sl9KUnqX6oyPovmJQkGQ
> g7V3HCEstz06aHh3gZegOwfanpLXRpf0duOcbFlqHQy8Q/49qwCoVZ86jLFdSlwB
> 0Ee1NnOhYWH4FTgCujtC1xAwKwJ7XjRhlH73/tLEUsOrf1zS8y4/azYlrz9j9K8X
> pgAHxk+QQxb2ff61kpzFJ/rdkkjx4gh0hZEBM47W81drbik30iKS6kcgS5Qz4jSE
> pZSXH+Gh63KBfJf87NrgTqX2Xhshgy0dZm2tpupsn2RUv6gpbNOCZ0WlNi7dgF27
> W7hXythvCbwBtACja310ADtXZLnldMQmNVXPv7fr9cyUtT9QYVn6GUWq6Ep/lkk1
> +bGcYgFeYshr8Y+TQghLitJcYw6iT5TEC1OpKOfbD7cqeMrngMY=
> =ulNB
> -----END PGP SIGNATURE-----
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
> 
> 

Thanks very much everyone, I really appreciate it!
Using getResourceAsStream instead of getResource fixed my problem. 

Thanks also Chris, for pointing out that I need to close the resource stream.
Below is my new implementation that now works in WebLogic 12c deployment.
cheers,
Shawn

//This works for weblogic. Because weblogic packages all the WAR contents into a JAR
//	there are no actual files in the JAR, so you have to use getResourceAsStream
InputStream resourceAsStream = null;
PDDocument document;
try {
    resourceAsStream =
	getClass().getClassLoader().getResourceAsStream("Level_A_FINAL_APPROVED_2017- 
   fieldsMappedToEntity.pdf");
   document = PDDocument.load(resourceAsStream);
} finally {
    resourceAsStream.close();
}


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Mime
View raw message