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

  1. Open the OFD source by passing the file path and OfdLoadOptions into the Document constructor. =======
  2. Open the OFD source by passing the file path and OfdLoadOptions into the Document constructor.

fix-java-docs

  1. Let Aspose.PDF parse the OFD package into the PDF document model.
  2. 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.

  1. Open the TeX source by passing the file path and TeXLoadOptions into the Document constructor.
  2. Let Aspose.PDF interpret the TeX markup and build the PDF layout during loading.
  3. 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.

  1. Open the PostScript source with PsLoadOptions in the Document constructor.
  2. Let Aspose.PDF translate the PostScript page-description stream into a PDF document model.
  3. 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.

  1. Open the EPS source with PsLoadOptions because EPS follows the same PostScript-based load path.
  2. Load the file into a Document so the page-description content is converted during import.
  3. 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.

  1. Open the EPUB source by passing the file path and EpubLoadOptions into the Document constructor.
  2. Let Aspose.PDF load the ebook structure and transform it into PDF pages.
  3. 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.

  1. Open the Markdown source by passing the file path and MdLoadOptions into the Document constructor.
  2. Let Aspose.PDF interpret the Markdown content and render it into PDF page content.
  3. 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.

  1. Read the plain-text source with UTF-8 decoding so the text content is available as a Java string.
  2. Create an empty Document and add a Page.
  3. Wrap the text in a TextFragment and add it to the page paragraphs collection.
  4. 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.

  1. Read all text lines from the input file so page-break markers can be inspected during conversion.
  2. Create an empty Document and configure each Page with margins and default text state.
  3. Resolve the monospaced font through FontRepository and add each line as a TextFragment.
  4. 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.

  1. Create PclLoadOptions and enable suppressed parsing errors when lenient import behavior is required.
  2. Open the PCL source by passing the file path and load options into the Document constructor.
  3. 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.

  1. Transform the XML source with the XSLT file into a temporary HTML file by calling the dedicated transformation method.
  2. Pass the generated HTML file into the existing HTML-to-PDF conversion function so the final PDF uses the standard HtmlLoadOptions workflow.
  3. Delete the temporary HTML file in the finally block after conversion completes.
  4. 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.

  1. Open the XPS source by passing the file path and XpsLoadOptions into the Document constructor.
  2. Let Aspose.PDF interpret the XPS page description during document loading.
  3. 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.

  1. Create XslFoLoadOptions with the XSLT path so the XML source can be transformed during loading.
  2. Configure the parsing error handling mode to throw immediately when invalid XSL-FO is encountered.
  3. Open the XML source in a Document with those load options.
  4. 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.

  1. Open the XML and XSLT input files as transformation sources.
  2. Create a Transformer from the XSLT stylesheet and run it against the XML source.
  3. 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()));
}