Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Converting XML file to PDF

Hi All,

I have a code where in it creates an XML file and eventually convert the file to a PDF. but I am getting this error below.

java.lang.StringIndexOutOfBoundsException: Range [0, -1) out of bounds for length 2
at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:112)
at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349)
at java.base/java.lang.String.checkBoundsBeginEnd(String.java:4608)
at java.base/java.lang.String.substring(String.java:2720)
at oracle.xdo.common.net.XDOStreamHandlerFactory.registerNewProtocols(XDOStreamHandlerFactory.java:155)
at oracle.xdo.template.FOProcessor.initParams(FOProcessor.java:451)
at oracle.xdo.template.FOProcessor.<init>(FOProcessor.java:411)
at com.JJDigitalSolution.salesandinventorysystem.CreateDeliveryForm.genPDF(CreateDeliveryForm.java:972)
at com.JJDigitalSolution.salesandinventorysystem.CreateDeliveryForm.GenerateOrder_jButton1ActionPerformed(CreateDeliveryForm.java:923)
at com.JJDigitalSolution.salesandinventorysystem.CreateDeliveryForm$9.actionPerformed(CreateDeliveryForm.java:498)

Here is the portion of my code:

            TransformerFactory transformerFactory = TransformerFactory.newInstance();  
           Transformer transformer = transformerFactory.newTransformer();  
           DOMSource source = new DOMSource(doc);  
           String date=df.format(dateobj).toString();  
           String file=OrderNumber+"\_"+OrderType+"\_"+date+".xml";  
           String filepdf=OrderNumber+"\_"+date;  
           System.out.println("GENERATING FILE="+file);  
           File filexml = new File(userhome+file);  
           System.out.println("filexml: "+filexml);  
           StreamResult result = new StreamResult(filexml);  
           System.out.println("SAUCE: "+source);  
           System.out.println("RESULT: "+result);  
           transformer.transform(source, result);  
             
           String\[\] param1= {userhome, userhome, userhome, OrderType+".rtf", file, filepdf+"\_"+OrderType+".pdf"};  
           genPDF(param1);

public void genPDF(String args\[\]) {  
   //String templ\_DIR = args\[0\]+"\\\\..\\\\template\\\\";  
   String templ\_DIR = args\[0\];  
   String in\_DIR = args\[1\];  
   String out\_DIR = args\[2\];  
   String a\_RTFFile = args\[3\];  
   String a\_XMLFile = args\[4\];  
   String a\_PDFFile = args\[5\];  
   String RTFfile = (new StringBuilder()).append(templ\_DIR).append(a\_RTFFile).toString();  
   String XMLfile = (new StringBuilder()).append(in\_DIR).append(a\_XMLFile).toString();  
   String PDFfile = (new StringBuilder()).append(out\_DIR).append(a\_PDFFile).toString();  
   String XSLfile = (new StringBuilder()).append(templ\_DIR).append(a\_RTFFile).append(".xsl").toString();  
   String XLIFFfile = (new StringBuilder()).append(templ\_DIR).append(a\_RTFFile).append(".xliff").toString();  
   System.out.println((new StringBuilder()).append("args-0(tmpl dir)=").append(templ\_DIR).toString());  
   System.out.println((new StringBuilder()).append("args-1  (in dir)=").append(in\_DIR).toString());  
   System.out.println((new StringBuilder()).append("args-2 (out dir)=").append(out\_DIR).toString());  
   System.out.println((new StringBuilder()).append("args-3(rtf file)=").append(a\_RTFFile).toString());  
   System.out.println((new StringBuilder()).append("args-4(xml file)=").append(a\_XMLFile).toString());  
   System.out.println((new StringBuilder()).append("args-5(pdf file)=").append(a\_PDFFile).toString());  
   try  
   {  
       RTFProcessor rtfp = new RTFProcessor(RTFfile);  
       rtfp.setOutput(XSLfile);  
       rtfp.setXLIFFOutput(XLIFFfile);  
       rtfp.process();  
       **FOProcessor p1 = new FOProcessor(); /\*\*ERROR OCCUR\*\*/**  
       p1.setXLIFF(XLIFFfile);  
       p1.setData(XMLfile);  
       p1.setTemplate(XSLfile);  
       p1.setOutputFormat(FOProcessor.FORMAT\_PDF);  
       p1.setOutput(PDFfile);  
       p1.generate();  
       File file = new File(XSLfile);  
       File file2 = new File(XLIFFfile);  
       file.delete();  
       file2.delete();  
   }  
   catch(Exception ex)  
   {  
       System.out.println(ex.toString());  
       ex.printStackTrace();  
   }  

}

Comments

Post Details

Added on Aug 30 2023
0 comments
416 views