com.manydesigns.elements.pdf.FormPdfExporter Maven / Gradle / Ivy
/*
* Copyright (C) 2005-2019 ManyDesigns srl. All rights reserved.
* http://www.manydesigns.com/
*
* Unless you have purchased a commercial license agreement from ManyDesigns srl,
* the following license terms apply:
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the GPL
* as it is applied to this software. View the full text of the
* exception in file OPEN-SOURCE-LICENSE.txt in the directory of this
* software distribution.
*
* This program is distributed WITHOUT ANY WARRANTY; and without the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
* or write to:
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330,
* Boston, MA 02111-1307 USA
*
*/
package com.manydesigns.elements.pdf;
import com.manydesigns.elements.fields.Field;
import com.manydesigns.elements.forms.FieldSet;
import com.manydesigns.elements.forms.Form;
import com.manydesigns.elements.xml.XmlBuffer;
import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
import javax.xml.transform.*;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;
import java.io.*;
/**
* @author Paolo Predonzani - [email protected]
* @author Angelo Lupo - [email protected]
* @author Giampiero Granatella - [email protected]
* @author Alessio Stalla - [email protected]
*/
public class FormPdfExporter {
public static final String copyright =
"Copyright (C) 2005-2019 ManyDesigns srl";
private final Form form;
private final Source xsltSource;
private String title;
public FormPdfExporter(Form form, Source xsltSource) {
this.form = form;
this.xsltSource = xsltSource;
}
public void export(OutputStream out) throws FOPException, IOException, TransformerException {
FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
// Setup XSLT
TransformerFactory Factory = TransformerFactory.newInstance();
Transformer transformer = Factory.newTransformer(xsltSource);
// Set the value of a in the stylesheet
transformer.setParameter("versionParam", "2.0");
// Setup input for XSLT transformation
Reader reader = composeXml();
Source src = new StreamSource(reader);
// Resulting SAX events (the generated FO) must be piped through to
// FOP
Result res = new SAXResult(fop.getDefaultHandler());
// Start XSLT transformation and FOP processing
transformer.transform(src, res);
reader.close();
out.flush();
}
/**
* Composes an XML document representing the current object.
*
* @return
* @throws java.io.IOException
*/
protected Reader composeXml() throws IOException {
XmlBuffer xb = new XmlBuffer();
xb.writeXmlHeader("UTF-8");
xb.openElement("class");
xb.openElement("table");
if (title != null) {
xb.write(title);
}
xb.closeElement("table");
for (FieldSet fieldset : form) {
xb.openElement("tableData");
xb.openElement("rows");
for (Field field : fieldset.fields()) {
xb.openElement("row");
xb.openElement("nameColumn");
xb.write(field.getLabel());
xb.closeElement("nameColumn");
xb.openElement("value");
xb.write(field.getStringValue());
xb.closeElement("value");
xb.closeElement("row");
}
xb.closeElement("rows");
xb.closeElement("tableData");
}
xb.closeElement("class");
return new StringReader(xb.toString());
}
public Form getForm() {
return form;
}
public Source getXsltSource() {
return xsltSource;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}