use of pcgen.util.fop.FopTask in project pcgen by PCGen.
the class BatchExporter method exportPartyToPDF.
/**
* 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 = new File(outFileName + (isTransformTemplate ? ".xml" : ".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 (final IOException e) {
Logging.errorPrint("BatchExporter.exportPartyToPDF failed", e);
return false;
} catch (final ExportException e) {
Logging.errorPrint("BatchExporter.exportPartyToPDF failed", e);
return false;
}
return true;
}
use of pcgen.util.fop.FopTask in project pcgen by PCGen.
the class BatchExporter method exportCharacterToPDF.
/**
* 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 = new File(outFileName + (isTransformTemplate ? ".xml" : ".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(//$NON-NLS-1$
"BatchExporter.exportCharacterToPDF failed: " + task.getErrorMessages());
return false;
}
} catch (final IOException e) {
//$NON-NLS-1$
Logging.errorPrint("BatchExporter.exportCharacterToPDF failed", e);
return false;
} catch (final ExportException e) {
//$NON-NLS-1$
Logging.errorPrint("BatchExporter.exportCharacterToPDF failed", e);
return false;
}
return true;
}
Aggregations