/** * Create a default character sheet output file name based on the export template type and the * character file name. The output file will be in the same folder as the character file. * * @param characterFilename The path to the character PCG file. * @return The default output file name. */ private String generateOutputFilename(String characterFilename) { File charFile = new File(characterFilename); String charname = charFile.getName(); String extension = ExportUtilities.getOutputExtension(exportTemplateFilename, isPdf); String outputName = charname.substring(0, charname.lastIndexOf('.')) + "." + extension; return new File(charFile.getParent(), outputName).getAbsolutePath(); }
/** * Get a temporary file name for outputting a character using a particular output template. * * @param templateFile The output template that will be used. * @return The temporary file, or null if it could not be created. */ public static File getTempOutputFilename(File templateFile) { String extension = ExportUtilities.getOutputExtension( templateFile.getName(), ExportUtilities.isPdfTemplate(templateFile)); try { // create a temporary file to view the character output return File.createTempFile( Constants.TEMPORARY_FILE_NAME, "." + extension, SettingsHandler.getTempPath()); } catch (IOException ioe) { ShowMessageDelegate.showMessageDialog( "Could not create temporary preview file.", "PCGen", MessageType.ERROR); Logging.errorPrint("Could not create temporary preview file.", ioe); return null; } }
/** * Write a PDF party sheet for the characters in the party to the output file. The party sheet * will be built according to the template file. If the output file exists it will be overwritten. * * @param party The already loaded party of characters to be output. * @param outFile The file to which the party 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 exportPartyToPDF(PartyFacade party, File outFile, File templateFile) { // We want the non pdf extension here for the intermediate file. String templateExtension = ExportUtilities.getOutputExtension(templateFile.getName(), false); 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) { exportParty(party, exportOutput); ByteArrayInputStream inputStream = new ByteArrayInputStream(byteOutputStream.toByteArray()); task = FopTask.newFopTask(inputStream, templateFile, fileStream); } else { SettingsHandler.setSelectedPartyPDFOutputSheet(templateFile.getAbsolutePath()); exportParty(party, templateFile, exportOutput); ByteArrayInputStream inputStream = new ByteArrayInputStream(byteOutputStream.toByteArray()); task = FopTask.newFopTask(inputStream, null, fileStream); } task.run(); } catch (IOException e) { Logging.errorPrint("BatchExporter.exportPartyToPDF failed", e); return false; } catch (ExportException e) { Logging.errorPrint("BatchExporter.exportPartyToPDF failed", e); return false; } return true; }