Edit in GitHubLog an issue

Create PDF

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)
Copied to your clipboard
1// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples
2// Run the sample:
3// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.createpdf.CreatePDFFromDOCX
4
5public class CreatePDFFromDOCX {
6
7 // Initialize the logger.
8 private static final Logger LOGGER = LoggerFactory.getLogger(CreatePDFFromDOCX .class);
9
10 public static void main(String[] args) {
11
12 try {
13
14 // Initial setup, create credentials instance.
15 Credentials credentials = Credentials.serviceAccountCredentialsBuilder()
16 .fromFile("pdfservices-api-credentials.json").build();
17
18 //Create an ExecutionContext using credentials and create a new operation instance.
19 ExecutionContext executionContext = ExecutionContext.create(credentials);
20 CreatePDFOperation createPdfOperation = CreatePDFOperation.createNew();
21
22 // Set operation input from a source file.
23 FileRef source = FileRef.createFromLocalFile("src/main/resources/createPDFInput.docx");
24 createPdfOperation.setInput(source);
25
26 // Execute the operation.
27 FileRef result = createPdfOperation.execute(executionContext);
28
29 // Save the result to the specified location.
30 result.saveAs("output/createPDFFromDOCX.pdf");
31
32 } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {
33 LOGGER.error("Exception encountered while executing
34 operation", ex);
35 }
36 }
37}

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)
Copied to your clipboard
1// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples
2// Run the sample:
3// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.createpdf.CreatePDFFromDOCXWithOptions
4
5public class CreatePDFFromDOCXWithOptions {
6
7 // Initialize the logger.
8 private static final Logger LOGGER = LoggerFactory.getLogger(CreatePDFFromDOCXWithOptions.class);
9
10 public static void main(String[] args) {
11
12 try {
13
14 // Initial setup, create credentials instance.
15 Credentials credentials = Credentials.serviceAccountCredentialsBuilder()
16 .fromFile("pdfservices-api-credentials.json")
17 .build();
18
19 //Create an ExecutionContext using credentials and create a new operation instance.
20 ExecutionContext executionContext = ExecutionContext.create(credentials);
21 CreatePDFOperation createPdfOperation = CreatePDFOperation.createNew();
22
23 // Set operation input from a source file.
24 FileRef source = FileRef.createFromLocalFile("src/main/resources/createPDFInput.docx");
25 createPdfOperation.setInput(source);
26
27 // Provide any custom configuration options for the operation.
28 setCustomOptions(createPdfOperation);
29
30 // Execute the operation.
31 FileRef result = createPdfOperation.execute(executionContext);
32
33 // Save the result to the specified location.
34 result.saveAs("output/createPDFFromDOCXWithOptionsOutput.pdf");
35
36 } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {
37 LOGGER.error("Exception encountered while executing operation", ex);
38 }
39 }
40
41 private static void setCustomOptions(CreatePDFOperation createPdfOperation) {
42 // Select the documentLanguage for input file.
43 SupportedDocumentLanguage documentLanguage = SupportedDocumentLanguage.EN_US;
44
45 // Set the desired Word-to-PDF conversion options.
46 CreatePDFOptions wordOptions = CreatePDFOptions.wordOptionsBuilder().
47 withDocumentLanguage(documentLanguage).
48 build();
49
50 createPdfOperation.setOptions(wordOptions);
51 }
52 }

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.

Copied to your clipboard
1// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples
2// Run the sample:
3// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.createpdf.CreatePDFFromStaticHTML
4
5public class CreatePDFFromStaticHTML {
6
7 // Initialize the logger.
8 private static final Logger LOGGER = LoggerFactory.getLogger(CreatePDFFromStaticHTML.class);
9
10 public static void main(String[] args) {
11
12 try {
13
14 // Initial setup, create credentials instance.
15 Credentials credentials = Credentials.serviceAccountCredentialsBuilder()
16 .fromFile("pdfservices-api-credentials.json")
17 .build();
18
19 //Create an ExecutionContext using credentials and create a new operation instance.
20 ExecutionContext executionContext = ExecutionContext.create(credentials);
21 CreatePDFOperation htmlToPDFOperation = CreatePDFOperation.createNew();
22
23 // Set operation input from a source file.
24 FileRef source = FileRef.createFromLocalFile("src/main/resources/createPDFFromStaticHtmlInput.zip");
25 htmlToPDFOperation.setInput(source);
26
27 // Provide any custom configuration options for the operation.
28 setCustomOptions(htmlToPDFOperation);
29
30 // Execute the operation.
31 FileRef result = htmlToPDFOperation.execute(executionContext);
32
33 // Save the result to the specified location.
34 result.saveAs("output/createPDFFromStaticHtmlOutput.pdf");
35
36 } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {
37 LOGGER.error("Exception encountered while executing operation", ex);
38 }
39 }
40
41 private static void setCustomOptions(CreatePDFOperation htmlToPDFOperation) {
42 // Define the page layout, in this case an 8 x 11.5 inch page (effectively portrait orientation).
43 PageLayout pageLayout = new PageLayout();
44 pageLayout.setPageSize(8, 11.5);
45
46 // Set the desired HTML-to-PDF conversion options.
47 CreatePDFOptions htmlToPdfOptions = CreatePDFOptions.htmlOptionsBuilder()
48 .includeHeaderFooter(true)
49 .withPageLayout(pageLayout)
50 .build();
51 htmlToPDFOperation.setOptions(htmlToPdfOptions);
52 }
53 }

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.

Copied to your clipboard
1// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples
2// Run the sample:
3// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.createpdf.CreatePDFFromHTMLWithInlineCSS
4
5 public class CreatePDFFromHTMLWithInlineCSS {
6
7 // Initialize the logger.
8 private static final Logger LOGGER = LoggerFactory.getLogger(CreatePDFFromStaticHTML.class);
9
10 public static void main(String[] args) {
11
12 try {
13
14 // Initial setup, create credentials instance.
15 Credentials credentials = Credentials.serviceAccountCredentialsBuilder()
16 .fromFile("pdfservices-api-credentials.json")
17 .build();
18
19 //Create an ExecutionContext using credentials and create a new operation instance.
20 ExecutionContext executionContext = ExecutionContext.create(credentials);
21 CreatePDFOperation htmlToPDFOperation = CreatePDFOperation.createNew();
22
23 // Set operation input from a source file.
24 FileRef source = FileRef.createFromLocalFile("src/main/resources/createPDFFromHTMLWithInlineCSSInput.html");
25 htmlToPDFOperation.setInput(source);
26
27 // Provide any custom configuration options for the operation.
28 setCustomOptions(htmlToPDFOperation);
29
30 // Execute the operation.
31 FileRef result = htmlToPDFOperation.execute(executionContext);
32
33 // Save the result to the specified location.
34 result.saveAs("output/createPDFFromHTMLWithInlineCSSOutput.pdf");
35
36 } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {
37 LOGGER.error("Exception encountered while executing operation", ex);
38 }
39 }
40
41 private static void setCustomOptions(CreatePDFOperation htmlToPDFOperation) {
42 // Define the page layout, in this case an 20 x 25 inch page (effectively portrait orientation).
43 PageLayout pageLayout = new PageLayout();
44 pageLayout.setPageSize(20, 25);
45
46 // Set the desired HTML-to-PDF conversion options.
47 CreatePDFOptions htmlToPdfOptions = CreatePDFOptions.htmlOptionsBuilder()
48 .includeHeaderFooter(true)
49 .withPageLayout(pageLayout)
50 .build();
51 htmlToPDFOperation.setOptions(htmlToPdfOptions);
52 }
53 }

Create a PDF File From HTML specified via URL

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

Copied to your clipboard
1// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples
2// Run the sample:
3// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.createpdf.CreatePDFFromURL
4
5 public class CreatePDFFromURL {
6
7 // Initialize the logger.
8 private static final Logger LOGGER = LoggerFactory.getLogger(CreatePDFFromStaticHTML.class);
9
10 public static void main(String[] args) {
11
12 try {
13
14 // Initial setup, create credentials instance.
15 Credentials credentials = Credentials.serviceAccountCredentialsBuilder()
16 .fromFile("pdfservices-api-credentials.json")
17 .build();
18
19 //Create an ExecutionContext using credentials and create a new operation instance.
20 ExecutionContext executionContext = ExecutionContext.create(credentials);
21 CreatePDFOperation htmlToPDFOperation = CreatePDFOperation.createNew();
22
23 // Set operation input from a source URL.
24 FileRef source = FileRef.createFromURL(new URL("https://www.adobe.io"));
25 htmlToPDFOperation.setInput(source);
26
27 // Provide any custom configuration options for the operation.
28 setCustomOptions(htmlToPDFOperation);
29
30 // Execute the operation.
31 FileRef result = htmlToPDFOperation.execute(executionContext);
32
33 // Save the result to the specified location.
34 result.saveAs("output/createPDFFromURLOutput.pdf");
35
36 } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {
37 LOGGER.error("Exception encountered while executing operation", ex);
38 }
39 }
40
41 private static void setCustomOptions(CreatePDFOperation htmlToPDFOperation) {
42 // Define the page layout, in this case an 20 x 25 inch page (effectively portrait orientation).
43 PageLayout pageLayout = new PageLayout();
44 pageLayout.setPageSize(20, 25);
45
46 // Set the desired HTML-to-PDF conversion options.
47 CreatePDFOptions htmlToPdfOptions = CreatePDFOptions.htmlOptionsBuilder()
48 .includeHeaderFooter(true)
49 .withPageLayout(pageLayout)
50 .build();
51 htmlToPDFOperation.setOptions(htmlToPdfOptions);
52 }
53 }

Create a PDF from dynamic HTML

To support workflows with dynamic data, CreatePDFFromDynamicHTML 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 CreatePDFFromDynamicHTML 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.

Copied to your clipboard
1// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples
2// Run the sample:
3// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.createpdf.CreatePDFFromDynamicHTML
4 public class CreatePDFFromDynamicHTML {
5
6 // Initialize the logger.
7 private static final Logger LOGGER = LoggerFactory.getLogger(CreatePDFFromDynamicHTML.class);
8
9 public static void main(String[] args) {
10
11 try {
12
13 // Initial setup, create credentials instance.
14 Credentials credentials = Credentials.serviceAccountCredentialsBuilder()
15 .fromFile("pdfservices-api-credentials.json")
16 .build();
17
18 //Create an ExecutionContext using credentials and create a new operation instance.
19 ExecutionContext executionContext = ExecutionContext.create(credentials);
20 CreatePDFOperation htmlToPDFOperation = CreatePDFOperation.createNew();
21
22 // Set operation input from a source file.
23 FileRef source = FileRef.createFromLocalFile("src/main/resources/createPDFFromDynamicHtmlInput.zip");
24 htmlToPDFOperation.setInput(source);
25
26 // Provide any custom configuration options for the operation.
27 setCustomOptions(htmlToPDFOperation);
28
29 // Execute the operation.
30 FileRef result = htmlToPDFOperation.execute(executionContext);
31
32 // Save the result to the specified location.
33 result.saveAs("output/createPDFFromDynamicHtmlOutput.pdf");
34
35 } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {
36 LOGGER.error("Exception encountered while executing operation", ex);
37 }
38 }
39
40 private static void setCustomOptions(CreatePDFOperation htmlToPDFOperation) {
41 // Define the page layout, in this case an 8 x 11.5 inch page (effectively portrait orientation).
42 PageLayout pageLayout = new PageLayout();
43 pageLayout.setPageSize(8, 11.5);
44
45 //Set the dataToMerge field that needs to be populated in the HTML before its conversion
46 JSONObject dataToMerge = new JSONObject();
47 dataToMerge.put("title","Create, Convert PDFs and More!");
48 dataToMerge.put("sub_title","Easily integrate PDF actions within your document workflows.");
49
50 // Set the desired HTML-to-PDF conversion options.
51 CreatePDFOptions htmlToPdfOptions = CreatePDFOptions.htmlOptionsBuilder()
52 .includeHeaderFooter(true)
53 .withPageLayout(pageLayout)
54 .withDataToMerge(dataToMerge)
55 .build();
56 htmlToPDFOperation.setOptions(htmlToPdfOptions);
57 }
58 }
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.