📄 exportertopdf.java
字号:
/* * Created on 18.09.2005 */package org.ganttproject.impex.htmlpdf;import java.awt.image.BufferedImage;import java.awt.image.RenderedImage;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.net.URL;import java.text.DateFormat;import java.util.ArrayList;import java.util.List;import java.util.Locale;import javax.imageio.ImageIO;import javax.xml.transform.OutputKeys;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerConfigurationException;import javax.xml.transform.sax.SAXResult;import javax.xml.transform.sax.SAXTransformerFactory;import javax.xml.transform.sax.TransformerHandler;import javax.xml.transform.stream.StreamResult;import javax.xml.transform.stream.StreamSource;import org.apache.fop.apps.Driver;import org.apache.fop.apps.FOPException;import org.apache.fop.apps.Options;import org.apache.fop.image.FopImageFactory;import org.eclipse.core.runtime.IProgressMonitor;import org.eclipse.core.runtime.IStatus;import org.eclipse.core.runtime.Platform;import org.eclipse.core.runtime.Status;import org.eclipse.core.runtime.jobs.Job;import org.ganttproject.impex.htmlpdf.fonts.FontRecord;import org.ganttproject.impex.htmlpdf.fonts.FontTriplet;import org.ganttproject.impex.htmlpdf.fonts.JDKFontLocator;import org.xml.sax.SAXException;import org.xml.sax.helpers.AttributesImpl;import net.sourceforge.ganttproject.GanttExportSettings;import net.sourceforge.ganttproject.IGanttProject;import net.sourceforge.ganttproject.export.ExportException;import net.sourceforge.ganttproject.export.Exporter;import net.sourceforge.ganttproject.export.TaskVisitor;import net.sourceforge.ganttproject.gui.UIFacade;import net.sourceforge.ganttproject.gui.options.model.GPOptionGroup;import net.sourceforge.ganttproject.language.GanttLanguage;import net.sourceforge.ganttproject.resource.HumanResource;import net.sourceforge.ganttproject.resource.ResourceManager;import net.sourceforge.ganttproject.task.ResourceAssignment;import net.sourceforge.ganttproject.task.Task;import net.sourceforge.ganttproject.task.TaskManager;public class ExporterToPDF extends ExporterBase implements Exporter { private static final String JPG_FORMAT_NAME = "jpg"; private PDFStylesheet myStylesheet; public String getFileTypeDescription() { return GanttLanguage.getInstance().getText("impex.pdf.description"); } public GPOptionGroup[] getSecondaryOptions() { return null; } public String getFileNamePattern() { return "pdf"; } public String proposeFileExtension() { return "pdf"; } public String[] getFileExtensions() { return new String[]{"pdf"}; } protected Job[] createJobs(File outputFile, List resultFiles) { ExportState state = new ExportState(outputFile); Job generateGanttChart = createGenerateGanttChartJob(state); Job generateResourceChart = createGenerateResourcechartJob(state); Job initializeFOP = createFOPInitializationJob(state); Job runTransormation = createTransformationJob(state); return new Job[]{generateGanttChart, generateResourceChart, initializeFOP, runTransormation}; } private Job createGenerateGanttChartJob(final ExportState state) { Job result = new ExportJob("generate gantt chart") { protected IStatus run(IProgressMonitor monitor) { if (monitor.isCanceled()) { Platform.getJobManager().cancel(ExporterBase.EXPORT_JOB_FAMILY); return Status.CANCEL_STATUS; } try { RenderedImage ganttChartImage = getGanttChart().getRenderedImage( new GanttExportSettings(true, true, true, true)); state.ganttChartImageFile = File.createTempFile( "ganttchart", ".jpg"); ImageIO.write(ganttChartImage, JPG_FORMAT_NAME, state.ganttChartImageFile); monitor.worked(1); } catch (Exception e) { cancel(); ExporterToPDF.this.getUIFacade().showErrorDialog(e); return Status.CANCEL_STATUS; } catch (OutOfMemoryError e) { cancel(); ExporterToPDF.this.getUIFacade().showErrorDialog(e); return Status.CANCEL_STATUS; } return Status.OK_STATUS; } }; return result; } private Job createGenerateResourcechartJob(final ExportState state) { Job result = new ExportJob("Generate resource chart") { protected IStatus run(IProgressMonitor monitor) { if (monitor.isCanceled()) { Platform.getJobManager().cancel(ExporterBase.EXPORT_JOB_FAMILY); return Status.CANCEL_STATUS; } try { RenderedImage resourceChartImage = getResourceChart().getRenderedImage( new GanttExportSettings(true, true, true, true)); File outputFile = File.createTempFile("resourcechart", ".jpg"); state.resourceChartImageFile = outputFile; ImageIO.write(resourceChartImage, JPG_FORMAT_NAME, outputFile); monitor.worked(1); } catch (Exception e) { cancel(); ExporterToPDF.this.getUIFacade().showErrorDialog(e); return Status.CANCEL_STATUS; } catch (OutOfMemoryError e) { cancel(); ExporterToPDF.this.getUIFacade().showErrorDialog(e); return Status.CANCEL_STATUS; } return Status.OK_STATUS; } }; return result; } private Job createFOPInitializationJob(final ExportState state) { Job result = new ExportJob("Initializing FOP") { protected IStatus run(IProgressMonitor monitor) { if (monitor.isCanceled()) { Platform.getJobManager().cancel(ExporterBase.EXPORT_JOB_FAMILY); return Status.CANCEL_STATUS; } try { Driver driver = new Driver(); driver.setRenderer(Driver.RENDER_PDF); Options options = createOptions(); FopImageFactory.resetCache(); state.driver = driver; monitor.worked(1); //throw new RuntimeException("Moooo!!!!!"); } catch (Exception e) { cancel(); ExporterToPDF.this.getUIFacade().showErrorDialog(e); return Status.CANCEL_STATUS; } return Status.OK_STATUS; } }; return result; } private Job createTransformationJob(final ExportState state) { Job result = new ExportJob("Generating PDF") { protected IStatus run(IProgressMonitor monitor) { if (monitor.isCanceled()) { Platform.getJobManager().cancel(ExporterBase.EXPORT_JOB_FAMILY); return Status.CANCEL_STATUS; } assert myStylesheet!=null; OutputStream out = null; try { out = new FileOutputStream(state.outputFile); state.driver.setOutputStream(out); TransformerHandler stylesheetHandler = createHandler(myStylesheet .getUrl().toString());// SAXTransformerFactory factory = getTransformerFactory();// TransformerHandler stylesheetHandler =// factory.newTransformerHandler();// Transformer transformer =// stylesheetHandler.getTransformer();// transformer.setOutputProperty(OutputKeys.ENCODING,// "UTF-8");// transformer.setOutputProperty(OutputKeys.INDENT, "yes");// transformer.setOutputProperty(// "{http://xml.apache.org/xslt}indent-amount", "4"); stylesheetHandler.setResult(new SAXResult(state.driver .getContentHandler()));// stylesheetHandler.setResult(new// StreamResult(System.out)); exportProject(state, stylesheetHandler); } catch (Exception e) { cancel(); ExporterToPDF.this.getUIFacade().showErrorDialog(e); return Status.CANCEL_STATUS; } finally { monitor.worked(1); if (out!=null) { try { out.flush(); out.close(); } catch(IOException e) { getUIFacade().showErrorDialog(e); } } } return Status.OK_STATUS; } }; return result; } private String i18n(String key) { String text = GanttLanguage.getInstance().getText(key); return GanttLanguage.getInstance().correctLabel(text); } protected void exportProject(ExportState state, TransformerHandler handler) throws SAXException, ExportException { DateFormat df = java.text.DateFormat.getDateTimeInstance( DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.getDefault()); handler.startDocument(); AttributesImpl attrs = new AttributesImpl(); addAttribute("xmlns:xsl", "http://www.w3.org/1999/XSL/Transform", attrs); addAttribute("xmlns:ganttproject", "http://ganttproject.sf.net/", attrs); addAttribute("version", "1.0", attrs); startElement("xsl:stylesheet", attrs, handler); // handler.startPrefixMapping("ganttproject",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -