Edit in GitHubLog an issue

Create PDF

Create PDFs from a variety of formats, including static and dynamic HTML; Microsoft Word, PowerPoint, and Excel; as well as text, image, Zip, and URL. Support for HTML to PDF, DOC to PDF, DOCX to PDF, PPT to PDF, PPTX to PDF, XLS to PDF, XLSX to PDF, TXT to PDF, RTF to PDF, BMP to PDF, JPEG to PDF, GIF to PDF, TIFF to PDF, PNG to PDF

REST API

See our public API Reference for :

Create a PDF

Use the sample below to create PDFs from Microsoft Office documents (Word, Excel and PowerPoint) and other supported file formats. While the example shows .docx file conversion, the SDK supports the following formats:

  • Microsoft Word (DOC, DOCX)
  • Microsoft PowerPoint (PPT, PPTX)
  • Microsoft Excel (XLS, XLSX)
  • Text (TXT, RTF)
  • Image (BMP, JPEG, GIF, TIFF, PNG)

Please refer the API usage guide to understand how to use our APIs.

Copied to your clipboard
// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples
// Run the sample:
// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.createpdf.CreatePDFFromDOCX
public class CreatePDFFromDOCX {
// Initialize the logger.
private static final Logger LOGGER = LoggerFactory.getLogger(CreatePDFFromDOCX.class);
public static void main(String[] args) {
try (InputStream inputStream = Files.newInputStream(new File("src/main/resources/createPDFInput.docx").toPath())) {
// Initial setup, create credentials instance
Credentials credentials = new ServicePrincipalCredentials(
System.getenv("PDF_SERVICES_CLIENT_ID"),
System.getenv("PDF_SERVICES_CLIENT_SECRET"));
// Creates a PDF Services instance
PDFServices pdfServices = new PDFServices(credentials);
// Creates an asset(s) from source file(s) and upload
Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.DOCX.getMediaType());
// Creates a new job instance
CreatePDFJob createPDFJob = new CreatePDFJob(asset);
// Submit the job and gets the job result
String location = pdfServices.submit(createPDFJob);
PDFServicesResponse<CreatePDFResult> pdfServicesResponse = pdfServices.getJobResult(location, CreatePDFResult.class);
// Get content from the resulting asset(s)
Asset resultAsset = pdfServicesResponse.getResult().getAsset();
StreamAsset streamAsset = pdfServices.getContent(resultAsset);
// Creates an output stream and copy stream asset's content to it
File.createDirectories(Paths.get("output/"));
OutputStream outputStream = Files.newOutputStream(new File("output/createPDFFromDOCX.pdf").toPath());
LOGGER.info("Saving asset at output/createPDFFromDOCX.pdf");
IOUtils.copy(streamAsset.getInputStream(), outputStream);
outputStream.close();
} catch (ServiceApiException | IOException | SDKException | ServiceUsageException ex) {
LOGGER.error("Exception encountered while executing the operation", ex);
}
}
}

Create PDF with DocumentLanguage

Use the sample below to create PDFs with supported documentLanguage from Microsoft Office documents (Word, Excel and PowerPoint). The example shows .docx file conversion with english as the language of the input file, the SDK supports the following formats:

  • Microsoft Word (DOC, DOCX)
  • Microsoft PowerPoint (PPT, PPTX)
  • Microsoft Excel (XLS, XLSX)
  • Text (TXT, RTF)

Please refer the API usage guide to understand how to use our APIs.

Copied to your clipboard
// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples
// Run the sample:
// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.createpdf.CreatePDFFromDOCXWithOptions
public class CreatePDFFromDOCXWithOptions {
// Initialize the logger.
private static final Logger LOGGER = LoggerFactory.getLogger(CreatePDFFromDOCXWithOptions.class);
public static void main(String[] args) {
try (InputStream inputStream = Files.newInputStream(new File("src/main/resources/createPDFInput.docx").toPath())) {
// Initial setup, create credentials instance
Credentials credentials = new ServicePrincipalCredentials(
System.getenv("PDF_SERVICES_CLIENT_ID"),
System.getenv("PDF_SERVICES_CLIENT_SECRET"));
// Creates a PDF Services instance
PDFServices pdfServices = new PDFServices(credentials);
// Creates an asset(s) from source file(s) and upload
Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.DOCX.getMediaType());
// Create parameters for the job
CreatePDFParams createPDFWordParams = CreatePDFParams.wordParamsBuilder().
withDocumentLanguage(DocumentLanguage.EN_US).
build();
// Creates a new job instance
CreatePDFJob createPDFJob = new CreatePDFJob(asset)
.setParams(createPDFWordParams);
// Submit the job and gets the job result
String location = pdfServices.submit(createPDFJob);
PDFServicesResponse<CreatePDFResult> pdfServicesResponse = pdfServices.getJobResult(location, CreatePDFResult.class);
// Get content from the resulting asset(s)
Asset resultAsset = pdfServicesResponse.getResult().getAsset();
StreamAsset streamAsset = pdfServices.getContent(resultAsset);
// Creates an output stream and copy stream asset's content to it
File.createDirectories(Paths.get("output/"));
OutputStream outputStream = Files.newOutputStream(new File("output/createPDFFromDOCXWithOptionsOutput.pdf").toPath());
LOGGER.info("Saving asset at output/createPDFFromDOCXWithOptionsOutput.pdf");
IOUtils.copy(streamAsset.getInputStream(), outputStream);
outputStream.close();
} catch (ServiceApiException | IOException | SDKException | ServiceUsageException ex) {
LOGGER.error("Exception encountered while executing operation", ex);
}
}
}

Create a PDF from static HTML

The sample below creates a PDF file from a static HTML file. The file must be local. Since HTML/web pages typically contain external assets, the input file must be a zip file containing an index.html at the top level of the archive as well as any dependencies such as images, css files, and so on.

Please refer the API usage guide to understand how to use our APIs.

Copied to your clipboard
// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples
// Run the sample:
// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.htmltopdf.StaticHTMLToPDF
public class StaticHTMLToPDF {
// Initialize the logger.
private static final Logger LOGGER = LoggerFactory.getLogger(StaticHTMLToPDF.class);
public static void main(String[] args) {
try (InputStream inputStream = Files.newInputStream(new File("src/main/resources/createPDFFromStaticHtmlInput.zip").toPath())) {
// Initial setup, create credentials instance
Credentials credentials = new ServicePrincipalCredentials(
System.getenv("PDF_SERVICES_CLIENT_ID"),
System.getenv("PDF_SERVICES_CLIENT_SECRET"));
// Creates a PDF Services instance
PDFServices pdfServices = new PDFServices(credentials);
// Creates an asset(s) from source file(s) and upload
Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.ZIP.getMediaType());
// Create parameters for the job
HTMLToPDFParams htmlToPDFParams = getHTMLToPDFParams();
// Creates a new job instance
HTMLToPDFJob htmLtoPDFJob = new HTMLToPDFJob(asset)
.setParams(htmlToPDFParams);
// Submit the job and gets the job result
String location = pdfServices.submit(htmLtoPDFJob);
PDFServicesResponse<HTMLToPDFResult> pdfServicesResponse = pdfServices.getJobResult(location, HTMLToPDFResult.class);
// Get content from the resulting asset(s)
Asset resultAsset = pdfServicesResponse.getResult().getAsset();
StreamAsset streamAsset = pdfServices.getContent(resultAsset);
// Creates an output stream and copy stream asset's content to it
File.createDirectories(Paths.get("output/"));
OutputStream outputStream = Files.newOutputStream(new File("output/staticHTMLToPDFOutput.pdf").toPath());
LOGGER.info("Saving asset at output/staticHTMLToPDFOutput.pdf");
IOUtils.copy(streamAsset.getInputStream(), outputStream);
outputStream.close();
} catch (ServiceApiException | IOException | SDKException | ServiceUsageException ex) {
LOGGER.error("Exception encountered while executing operation", ex);
}
}
private static HTMLToPDFParams getHTMLToPDFParams() {
// Define the page layout, in this case an 8 x 11.5 inch page (effectively portrait orientation)
PageLayout pageLayout = new PageLayout();
pageLayout.setPageSize(8, 11.5);
return new HTMLToPDFParams.Builder()
.includeHeaderFooter(true).withPageLayout(pageLayout)
.build();
}
}

Create a PDF from static HTML with inline CSS

The sample below creates a PDF file from a static HTML file with inline CSS. The file must be local.

Please refer the API usage guide to understand how to use our APIs.

Copied to your clipboard
// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples
// Run the sample:
// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.htmltopdf.HTMLWithInlineCSSToPDF
public class HTMLWithInlineCSSToPDF {
// Initialize the logger.
private static final Logger LOGGER = LoggerFactory.getLogger(HTMLWithInlineCSSToPDF.class);
public static void main(String[] args) {
try (InputStream inputStream = Files.newInputStream(new File("src/main/resources/createPDFFromHTMLWithInlineCSSInput.html").toPath())) {
// Initial setup, create credentials instance
Credentials credentials = new ServicePrincipalCredentials(
System.getenv("PDF_SERVICES_CLIENT_ID"),
System.getenv("PDF_SERVICES_CLIENT_SECRET"));
// Creates a PDF Services instance
PDFServices pdfServices = new PDFServices(credentials);
// Creates an asset(s) from source file(s) and upload
Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.HTML.getMediaType());
// Create parameters for the job
HTMLToPDFParams htmlToPDFParams = getHTMLToPDFParams();
// Creates a new job instance
HTMLToPDFJob htmLtoPDFJob = new HTMLToPDFJob(asset)
.setParams(htmlToPDFParams);
// Submit the job and gets the job result
String location = pdfServices.submit(htmLtoPDFJob);
PDFServicesResponse<HTMLToPDFResult> pdfServicesResponse = pdfServices.getJobResult(location, HTMLToPDFResult.class);
// Get content from the resulting asset(s)
Asset resultAsset = pdfServicesResponse.getResult().getAsset();
StreamAsset streamAsset = pdfServices.getContent(resultAsset);
// Creates an output stream and copy stream asset's content to it
File.createDirectories(Paths.get("output/"));
OutputStream outputStream = Files.newOutputStream(new File("output/htmlWithInlineCSSToPDFOutput.pdf").toPath());
LOGGER.info("Saving asset at output/htmlWithInlineCSSToPDFOutput.pdf");
IOUtils.copy(streamAsset.getInputStream(), outputStream);
outputStream.close();
} catch (ServiceApiException | IOException | SDKException | ServiceUsageException ex) {
LOGGER.error("Exception encountered while executing operation", ex);
}
}
private static HTMLToPDFParams getHTMLToPDFParams() {
// Define the page layout, in this case an 8 x 11.5 inch page (effectively portrait orientation)
PageLayout pageLayout = new PageLayout();
pageLayout.setPageSize(20, 25);
return new HTMLToPDFParams.Builder()
.includeHeaderFooter(true).withPageLayout(pageLayout)
.build();
}
}

Create a PDF File From HTML specified via URL

The sample below creates a PDF file from a HTML file specified via URL.

Please refer the API usage guide to understand how to use our APIs.

Copied to your clipboard
// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples
// Run the sample:
// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.htmltopdf.HTMLToPDFFromURL
public class HTMLToPDFFromURL {
// Initialize the logger.
private static final Logger LOGGER = LoggerFactory.getLogger(HTMLToPDFFromURL.class);
public static void main(String[] args) {
try {
// Initial setup, create credentials instance
Credentials credentials = new ServicePrincipalCredentials(
System.getenv("PDF_SERVICES_CLIENT_ID"),
System.getenv("PDF_SERVICES_CLIENT_SECRET"));
// Creates a PDF Services instance
PDFServices pdfServices = new PDFServices(credentials);
String htmlURL = "<HTML URL>";
// Create parameters for the job
HTMLToPDFParams htmlToPDFParams = getHTMLToPDFParams();
// Creates a new job instance
HTMLToPDFJob htmLtoPDFJob = new HTMLToPDFJob(htmlURL)
.setParams(htmlToPDFParams);
// Submit the job and gets the job result
String location = pdfServices.submit(htmLtoPDFJob);
PDFServicesResponse<HTMLToPDFResult> pdfServicesResponse = pdfServices.getJobResult(location, HTMLToPDFResult.class);
// Get content from the resulting asset(s)
Asset resultAsset = pdfServicesResponse.getResult().getAsset();
StreamAsset streamAsset = pdfServices.getContent(resultAsset);
// Creates an output stream and copy stream asset's content to it
File.createDirectories(Paths.get("output/"));
OutputStream outputStream = Files.newOutputStream(new File("output/htmlToPDFFromURLOutput.pdf").toPath());
LOGGER.info("Saving asset at output/htmlToPDFFromURLOutput.pdf");
IOUtils.copy(streamAsset.getInputStream(), outputStream);
} catch (ServiceApiException | IOException | SDKException | ServiceUsageException ex) {
LOGGER.error("Exception encountered while executing operation", ex);
}
}
private static HTMLToPDFParams getHTMLToPDFParams() {
// Define the page layout, in this case an 8 x 11.5 inch page (effectively portrait orientation)
PageLayout pageLayout = new PageLayout();
pageLayout.setPageSize(20, 25);
return new HTMLToPDFParams.Builder()
.includeHeaderFooter(true).withPageLayout(pageLayout)
.build();
}
}

Create a PDF from dynamic HTML

To support workflows with dynamic data, DynamicHTMLToPDF creates PDFs from dynamic HTML. It's a common scenario for enterprise to provide end users with an HTML template with form fields. This API allows you to capture the users unique data entries and then save it as PDF. Collected data is stored in a JSON file, and the source HTML file must include <script src='./json.js' type='text/javascript'></script>. Refer to the API docs for usage.

The sample DynamicHTMLToPDF converts a zip file, containing the input HTML file and its resources, along with the input data to a PDF file. The input data is used by the JavaScript in the HTML file to manipulate the HTML DOM, thus effectively updating the source HTML file. This mechanism can be used to provide data to the template HTML dynamically prior to PDF conversion.

Please refer the API usage guide to understand how to use our APIs.

Copied to your clipboard
// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples
// Run the sample:
// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.htmltopdf.DynamicHTMLToPDF
public class DynamicHTMLToPDF {
// Initialize the logger.
private static final Logger LOGGER = LoggerFactory.getLogger(DynamicHTMLToPDF.class);
public static void main(String[] args) {
try (InputStream inputStream = Files.newInputStream(new File("src/main/resources/createPDFFromDynamicHtmlInput.zip").toPath())) {
// Initial setup, create credentials instance
Credentials credentials = new ServicePrincipalCredentials(
System.getenv("PDF_SERVICES_CLIENT_ID"),
System.getenv("PDF_SERVICES_CLIENT_SECRET"));
// Creates a PDF Services instance
PDFServices pdfServices = new PDFServices(credentials);
// Creates an asset(s) from source file(s) and upload
Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.ZIP.getMediaType());
// Create parameters for the job
HTMLToPDFParams htmlToPDFParams = getHTMLToPDFParams();
// Creates a new job instance
HTMLToPDFJob htmLtoPDFJob = new HTMLToPDFJob(asset)
.setParams(htmlToPDFParams);
// Submit the job and gets the job result
String location = pdfServices.submit(htmLtoPDFJob);
PDFServicesResponse<HTMLToPDFResult> pdfServicesResponse = pdfServices.getJobResult(location, HTMLToPDFResult.class);
// Get content from the resulting asset(s)
Asset resultAsset = pdfServicesResponse.getResult().getAsset();
StreamAsset streamAsset = pdfServices.getContent(resultAsset);
// Creates an output stream and copy stream asset's content to it
File.createDirectories(Paths.get("output/"));
OutputStream outputStream = Files.newOutputStream(new File("output/dynamicHTMLToPDFOutput.pdf").toPath());
LOGGER.info("Saving asset at output/dynamicHTMLToPDFOutput.pdf");
IOUtils.copy(streamAsset.getInputStream(), outputStream);
outputStream.close();
} catch (ServiceApiException | IOException | SDKException | ServiceUsageException ex) {
LOGGER.error("Exception encountered while executing operation", ex);
}
}
private static HTMLToPDFParams getHTMLToPDFParams() {
// Define the page layout, in this case an 8 x 11.5 inch page (effectively portrait orientation)
PageLayout pageLayout = new PageLayout();
pageLayout.setPageSize(8, 11.5);
// Sets the dataToMerge field that needs to be populated in the HTML before its conversion
JSONObject dataToMerge = new JSONObject();
dataToMerge.put("title","Create, Convert PDFs and More!");
dataToMerge.put("sub_title","Easily integrate PDF actions within your document workflows.");
return new HTMLToPDFParams.Builder()
.includeHeaderFooter(true).withPageLayout(pageLayout).withDataToMerge(dataToMerge)
.build();
}
}
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.