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 }