/** * Write a PDF character sheet for the character to the output file. The character sheet will be * built according to the template file. If the output file exists it will be overwritten. * * @param character The already loaded character to be output. * @param outFile The file to which the character sheet is to be written. * @param templateFile The file that has the export template definition. * @return true if the export was successful, false if it failed in some way. */ public static boolean exportCharacterToPDF( CharacterFacade character, File outFile, File templateFile) { String templateExtension = FilenameUtils.getExtension(templateFile.getName()); boolean isTransformTemplate = "xslt".equalsIgnoreCase(templateExtension) || "xsl".equalsIgnoreCase(templateExtension); boolean useTempFile = PCGenSettings.OPTIONS_CONTEXT.initBoolean( PCGenSettings.OPTION_GENERATE_TEMP_FILE_WITH_PDF, false); String outFileName = FilenameUtils.removeExtension(outFile.getAbsolutePath()); File tempFile = isTransformTemplate ? new File(outFileName + ".xml") : new File(outFileName + ".fo"); try (BufferedOutputStream fileStream = new BufferedOutputStream(new FileOutputStream(outFile)); ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); OutputStream exportOutput = useTempFile // Output to both the byte stream and to the temp file. ? new TeeOutputStream(byteOutputStream, new FileOutputStream(tempFile)) : byteOutputStream) { FopTask task; if (isTransformTemplate) { exportCharacter(character, exportOutput); ByteArrayInputStream inputStream = new ByteArrayInputStream(byteOutputStream.toByteArray()); task = FopTask.newFopTask(inputStream, templateFile, fileStream); } else { exportCharacter(character, templateFile, exportOutput); ByteArrayInputStream inputStream = new ByteArrayInputStream(byteOutputStream.toByteArray()); task = FopTask.newFopTask(inputStream, null, fileStream); } character.setDefaultOutputSheet(true, templateFile); task.run(); if (StringUtils.isNotBlank(task.getErrorMessages())) { Logging.errorPrint( "BatchExporter.exportCharacterToPDF failed: " //$NON-NLS-1$ + task.getErrorMessages()); return false; } } catch (IOException e) { Logging.errorPrint("BatchExporter.exportCharacterToPDF failed", e); // $NON-NLS-1$ return false; } catch (ExportException e) { Logging.errorPrint("BatchExporter.exportCharacterToPDF failed", e); // $NON-NLS-1$ return false; } return true; }
@Override protected AWTRenderer doInBackground() throws Exception { URI osPath = new File(ConfigurationSettings.getOutputSheetsDir()).toURI(); File xsltFile = new File(osPath.resolve(uri)); FOUserAgent userAgent = FopTask.getFactory().newFOUserAgent(); AWTRenderer renderer = new AWTRenderer(userAgent, null, false, false); PipedOutputStream out = new PipedOutputStream(); FopTask task = FopTask.newFopTask(new PipedInputStream(out), xsltFile, renderer); Thread thread = new Thread(task, "fop-preview"); thread.setDaemon(true); thread.start(); BatchExporter.exportCharacter(character, out); try { thread.join(); } catch (InterruptedException ex) { // pass on the interrupt and hope it stops thread.interrupt(); } return renderer; }
/** * Exports a character to an OuputStream using the default template for the character's game mode. * This is more generic method than writing to a file and the same effect can be achieved by * passing in a FileOutputStream. * * @param character the loaded CharacterFacade to export * @param outputStream the OutputStream that the character will be exported to * @throws IOException * @throws ExportException */ public static void exportCharacter(CharacterFacade character, OutputStream outputStream) throws IOException, ExportException { exportCharacter(character, getXMLTemplate(character), outputStream); }