Convert Other File Formats to PDF in Java
Aspose.PDF for Java supports conversion from document, markup, and page-description formats into PDF.
Convert OFD to PDF
Use this example when an OFD document should be converted into PDF.
«««< HEAD
- Open the OFD source by passing the file path and
OfdLoadOptionsinto theDocumentconstructor. ======= - Open the OFD source by passing the file path and
OfdLoadOptionsinto theDocumentconstructor.
fix-java-docs
- Let Aspose.PDF parse the OFD package into the PDF document model.
- Save the resulting PDF to the target output path.
public static void convertOfdToPdf(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString(), new OfdLoadOptions())) {
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert TeX to PDF
Use this example when TeX content should be rendered directly as PDF.
- Open the TeX source by passing the file path and
TeXLoadOptionsinto theDocumentconstructor. - Let Aspose.PDF interpret the TeX markup and build the PDF layout during loading.
- Save the generated PDF.
public static void convertTexToPdf(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString(), new com.aspose.pdf.TeXLoadOptions())) {
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert PostScript to PDF
Use this example when a PostScript file should be converted into a PDF document.
- Open the PostScript source with
PsLoadOptionsin theDocumentconstructor. - Let Aspose.PDF translate the PostScript page-description stream into a PDF document model.
- Save the converted PDF file.
public static void convertPostScripToPdf(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString(), new PsLoadOptions())) {
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert EPS to PDF
Use this example when an Encapsulated PostScript file should be converted to PDF.
- Open the EPS source with
PsLoadOptionsbecause EPS follows the same PostScript-based load path. - Load the file into a
Documentso the page-description content is converted during import. - Save the output PDF.
public static void convertEpsToPdf(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString(), new PsLoadOptions())) {
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert EPUB to PDF
Use this example when an EPUB eBook should be converted into PDF.
- Open the EPUB source by passing the file path and
EpubLoadOptionsinto theDocumentconstructor. - Let Aspose.PDF load the ebook structure and transform it into PDF pages.
- Save the converted PDF.
public static void convertEpubToPdf(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString(), new EpubLoadOptions())) {
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert Markdown to PDF
Use this example when Markdown content should be rendered and saved as PDF.
- Open the Markdown source by passing the file path and
MdLoadOptionsinto theDocumentconstructor. - Let Aspose.PDF interpret the Markdown content and render it into PDF page content.
- Save the output PDF file.
public static void convertMdToPdf(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString(), new MdLoadOptions())) {
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert text to PDF with a simple workflow
Use this example when a plain text file should be quickly converted to PDF.
- Read the plain-text source with UTF-8 decoding so the text content is available as a Java string.
- Create an empty
Documentand add aPage. - Wrap the text in a
TextFragmentand add it to the page paragraphs collection. - Save the generated PDF.
public static void convertTxtToPdfSimple(Path inputFile, Path outputFile) throws Exception {
String textContent = Files.readString(inputFile, StandardCharsets.UTF_8);
try (Document document = new Document()) {
Page page = document.getPages().add();
page.getParagraphs().add(new TextFragment(textContent));
page.close();
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert text to PDF with advanced options
Use this example when plain text should be converted with additional layout or encoding options.
- Read all text lines from the input file so page-break markers can be inspected during conversion.
- Create an empty
Documentand configure eachPagewith margins and default text state. - Resolve the monospaced font through
FontRepositoryand add each line as aTextFragment. - Save the output file after the page-building loop completes.
public static void convertTxtToPdf(Path inputFile, Path outputFile) throws Exception {
List<String> lines = Files.readAllLines(inputFile);
try (Document document = new Document()) {
com.aspose.pdf.Page page = document.getPages().add();
page.getPageInfo().getMargin().setLeft(20);
page.getPageInfo().getMargin().setRight(10);
page.getPageInfo().getDefaultTextState().setFont(FontRepository.findFont("Courier New"));
page.getPageInfo().getDefaultTextState().setFontSize(12);
int pageCount = 1;
for (String line : lines) {
if (!line.isEmpty() && line.charAt(0) == '\f') {
page = document.getPages().add();
page.getPageInfo().getMargin().setLeft(20);
page.getPageInfo().getMargin().setRight(10);
page.getPageInfo().getDefaultTextState().setFont(FontRepository.findFont("Courier New"));
page.getPageInfo().getDefaultTextState().setFontSize(12);
pageCount++;
if (pageCount == 4) {
break;
}
} else {
page.getParagraphs().add(new TextFragment(line));
}
}
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert PCL to PDF
Use this example when a PCL print stream should be converted into PDF.
- Create
PclLoadOptionsand enable suppressed parsing errors when lenient import behavior is required. - Open the PCL source by passing the file path and load options into the
Documentconstructor. - Save the result as PDF.
public static void convertPclToPdf(Path inputFile, Path outputFile) {
PclLoadOptions loadOptions = new PclLoadOptions();
loadOptions.setSupressErrors(true);
try (Document document = new Document(inputFile.toString(), loadOptions)) {
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert XML to PDF through XSLT and HTML
Use this example when XML data should be transformed before final PDF generation.
- Transform the XML source with the XSLT file into a temporary HTML file by calling the dedicated transformation method.
- Pass the generated HTML file into the existing HTML-to-PDF conversion function so the final PDF uses the standard
HtmlLoadOptionsworkflow. - Delete the temporary HTML file in the
finallyblock after conversion completes. - Save the generated PDF file.
public static void convertXmlToPdf(Path xsltFile, Path xmlFile, Path outputFile) throws Exception {
Path htmlFile = Files.createTempFile("aspose-pdf-xml-", ".html");
try {
transformXmlToHtml(xmlFile, xsltFile, htmlFile);
HtmlToPdfExamples.convertHtmlToPdf(htmlFile, outputFile);
} finally {
Files.deleteIfExists(htmlFile);
}
System.out.println(xmlFile + " converted into " + outputFile);
}
Convert XPS to PDF
Use this example when an XPS document should be converted into PDF.
- Open the XPS source by passing the file path and
XpsLoadOptionsinto theDocumentconstructor. - Let Aspose.PDF interpret the XPS page description during document loading.
- Save the converted PDF.
public static void convertXpsToPdf(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString(), new XpsLoadOptions())) {
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert XSL-FO to PDF
Use this example when XSL-FO content should be rendered as PDF.
- Create
XslFoLoadOptionswith the XSLT path so the XML source can be transformed during loading. - Configure the parsing error handling mode to throw immediately when invalid XSL-FO is encountered.
- Open the XML source in a
Documentwith those load options. - Save the resulting PDF document.
public static void convertXslFoToPdf(Path xsltFile, Path xmlFile, Path outputFile) {
XslFoLoadOptions loadOptions = new XslFoLoadOptions(xsltFile.toString());
loadOptions.setParsingErrorsHandlingType(XslFoLoadOptions.ParsingErrorsHandlingTypes.ThrowExceptionImmediately);
try (Document document = new Document(xmlFile.toString(), loadOptions)) {
document.save(outputFile.toString());
}
System.out.println(xmlFile + " converted into " + outputFile);
}
Transform XML to intermediate HTML
Use this method when XML data must be transformed to HTML before the final PDF conversion step.
- Open the XML and XSLT input files as transformation sources.
- Create a
Transformerfrom the XSLT stylesheet and run it against the XML source. - Write the transformed HTML file to disk so the downstream PDF conversion function can load it.
private static void transformXmlToHtml(Path xmlFile, Path xsltFile, Path htmlFile) throws Exception {
Transformer transformer = TransformerFactory.newInstance()
.newTransformer(new StreamSource(xsltFile.toFile()));
transformer.transform(new StreamSource(xmlFile.toFile()), new StreamResult(htmlFile.toFile()));
}