XML转换为PDF | iText | java.net.MalformedURLException

时间:2018-11-23 00:33:20

标签: java xml pdf xml-parsing itext

我尝试了不同的“ input.xml”和“ tagMap.xml”文件。 调整结构和标签...但是我不断收到“ MalformedURLException”

除使用iText解决此问题外,任何其他建议均受欢迎。只要输入文件始终是带有内容的XML(input.xml)和带有tagMap的另一个XML(tagMap.xml)。

此问题来自需要升级我们的系统。 Weblogic服务器8到12c,Java 1.4到1.8 ...无意更改应用程序上的任何功能,代码/库似乎不兼容。

(注意:出于演示目的,我已经减少了“ input.xml”和“ tagMap.xml”的所有Java代码+内容/结构。)

有什么想法吗?


    package sandbox;

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import com.lowagie.text.*;
    import com.lowagie.text.pdf.*;
    import com.lowagie.text.xml.XmlParser;

    public class Main {

    public static void main(String[] args) {
            String testPath = "D:"+File.separator+"JavaTest";
    // step 1: creation of a document-object
          Document document = new Document(PageSize.A4, 80, 50, 30, 65);
    // step 2: we create a writer that listens to the document
     try {
            PdfWriter.getInstance(document, new FileOutputStream
                      (testPath+File.separator+"outPut.pdf"));
    // step 3: we parse the document
          XmlParser.parse(document, testPath+ File.separator+ "input.xml"
                  , testPath+ File.separator+ "tagMap.xml");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }

        }

    }

堆栈跟踪:

Exception in thread "main" ExceptionConverter: java.net.MalformedURLException
at java.net.URL.<init>(URL.java:627)
at java.net.URL.<init>(URL.java:490)
at java.net.URL.<init>(URL.java:439)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:620)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:148)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:805)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:770)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:327)
at com.lowagie.text.xml.TagMap.init(Unknown Source)
at com.lowagie.text.xml.TagMap.<init>(Unknown Source)
at com.lowagie.text.xml.XmlParser.go(Unknown Source)
at com.lowagie.text.xml.XmlParser.parse(Unknown Source)
at sandbox.Main.main(Main.java:32)
Caused by: java.lang.NullPointerException
at java.net.URL.<init>(URL.java:532)
... 15 more

input.xml内容:

<?xml version="1.0"?>

<GREETING>
    <TEXT>Hello World PDF Test</TEXT>
</GREETING>

tagMap.xml内容:

<tagmap>
<tag name="iText" alias="GREETING" />
<tag name="paragraph" alias="TEXT">
    <attribute name="leading" value="14" />
    <attribute name="size" value="10" />
    <attribute name="style" value="italic" />
    <attribute name="align" value="left" />
</tag>
</tagmap>

使用: com.lowagie.text.xml.XmlParser

public static void parse(DocListener document,
                     java.lang.String file,
                     java.lang.String tagmap)
Parses a given file that validates with the iText DTD and writes the content to a document.
Parameters:
    document - The document that will listen to the parser
    file - The path to a file with the content
    tagmap - A user defined tagmap

1 个答案:

答案 0 :(得分:0)

这不是我的问题的实际解决方案,但是如果有人来找我,请注明我的工作: 我要做的是删除所有iText库的使用,并用apache FOP 2.3替换PDF处理 实施是直接的。我必须生成.xsl(模板)和.xml(包含输入变量)。