0

我正在尝试从 xpage 的内容创建 PDF。我遵循 Paul Calhoun 在 9 #102 的 Notes 中使用的格式。我能够为视图创建 PDF,但无法为文档创建 PDF。我不认为错误出现在 Paul 的代码中,所以我没有将它包括在这里,尽管如果需要我可以。

为了生成要显示的 XML,我使用 java 中文档类的 generateXML() 方法。我得到后端文档的句柄,然后返回 XML。XML 看起来格式正确,顶级选项卡是<document>. 我将此 XML 传递给使用 Apache FOP 的转换器。我的所有代码都包含在 xAgent 的 beforeRenderResponse 中。

我使用的 XSL 样式表是一个精简的版本,只是为了让概念证明可以工作。我将包含它,因为问题可能出在此代码上。我对 XSL 完全陌生。

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet
    version="1.1"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format"
    exclude-result-prefixes="fo">
    <xsl:output
        method="xml"
        version="1.0"
        omit-xml-declaration="no"
        indent="yes" />
    <xsl:param
        name="versionParam"
        select="'1.0'" />

    <xsl:template match="document">
        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
            <fo:layout-master-set>
                <fo:simple-page-master
                    master-name="outpage"
                    page-height="11in"
                    page-width="8.5in"
                    margin-top="1in"
                    margin-bottom="1in"
                    margin-left="1in"
                    margin-right="1in">
                    <fo:region-body />
                </fo:simple-page-master>
            </fo:layout-master-set>
            <fo:page-sequence master-reference="A4">
                <fo:block
                        font-size="16pt"
                        font-weight="bold"
                        space-after="5mm">
                        Apache FOP Proof of Concept.
                    </fo:block>
            </fo:page-sequence>
            </fo:root>
    </xsl:template>
</xsl:stylesheet>

在日志文件中,我收到消息:

FATAL ERROR:  'com.ibm.xtq.common.utils.WrappedRuntimeException: D:\Program Files\IBM\Domino\<document form='PO'>

该错误在尝试转换的日志中回显整个 XML,并以:

 (The filename, directory name, or volume label syntax is incorrect.)'

请注意,Domino 正在尝试将 XML 包含在路径中。我知道这是错误的,但不知道如何解决它。

编辑:这是运行转换的 Java 类。此代码来自 Paul Calhoun 的演示。

import java.io.OutputStream;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;

public class DominoXMLFO2PDF {
    public static void getPDF(OutputStream pdfout,String xml,String xslt, Boolean authReq, String usernamepass) {
        try {
            //System.out.println("Transforming...");
            Source xmlSource,xsltSource;  //Source xmlSource = new StreamSource("http://localhost/APCC.nsf/Main?ReadViewEntries&count=999&ResortAscending=2");
            xmlSource = new StreamSource(xml);  //Source xsltSource = new StreamSource("http://localhost/APCC.nsf/viewdata.xsl");
            xsltSource = new StreamSource(xslt);// configure fopFactory as desired              

            final FopFactory fopFactory = FopFactory.newInstance();

            FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
            // configure foUserAgent as desired

            // Setup output
            // OutputStream out = pdfout;
            // out = new java.io.BufferedOutputStream(out);

            try {
                // Construct fop with desired output format
                Fop fop = fopFactory.newFop(org.apache.xmlgraphics.util.MimeConstants.MIME_PDF,foUserAgent, pdfout);
                // Setup XSLT
                TransformerFactory factory = TransformerFactory.newInstance();
                Transformer transformer = factory.newTransformer(xsltSource);
                //transformer.setParameter("versionParam", "Company List"); // Set the value of a <param> in the stylesheet
                Source src = xmlSource; // Setup input for XSLT transformation
                Result res = new SAXResult(fop.getDefaultHandler()); // Resulting SAX events (the generated FO) must be piped through to FOP
                transformer.transform(src, res); // Start XSLT transformation and FOP processing

            } catch (Exception e) {

            } finally {
            }
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }
}

使用此行在 xAgent 中调用此代码:

var retOutput = jce.getPDF(pageOutput, xmlsource, xsltsource, authReq, usernamepass);

xmlsource 使用以下行设置,其中方法使用 Document.generateXML() 返回 XML:

var xmlsource = statusBean.generateXML(POdata, sessionScope.unidPDF);
4

1 回答 1

1

您的问题是 XMLSource!当您查看 Paul 的代码时:

Source xmlSource = new StreamSource("http://localhost/APCC.nsf/Main?ReadViewEntries");

指向检索 XML 的 URL。另一方面,您的代码:

 xmlsource = statusBean.generateXML(POdata, sessionScope.unidPDF);

包含XML。因此,您需要更改为:

   String xmlstring = statusBean.generateXML(POdata, sessionScope.unidPDF);
   Source xmlsource = new StreamSource(new java.io.StringReader(xmlstring));

我强烈建议您尝试将所有 Java 保留在一个 java 类中,这样您就不需要在 SSJS 中包装/向上包装对象。也看看我关于 FO 的系列

于 2013-11-07T02:58:31.730 回答