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
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 (InputStream inputStream = Files.newInputStream(new File("src/main/resources/createPDFInput.docx").toPath())) {
13 // Initial setup, create credentials instance
14 Credentials credentials = new ServicePrincipalCredentials(
15 System.getenv("PDF_SERVICES_CLIENT_ID"),
16 System.getenv("PDF_SERVICES_CLIENT_SECRET"));
17
18 // Creates a PDF Services instance
19 PDFServices pdfServices = new PDFServices(credentials);
20
21 // Creates an asset(s) from source file(s) and upload
22 Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.DOCX.getMediaType());
23
24 // Creates a new job instance
25 CreatePDFJob createPDFJob = new CreatePDFJob(asset);
26
27 // Submit the job and gets the job result
28 String location = pdfServices.submit(createPDFJob);
29 PDFServicesResponse<CreatePDFResult> pdfServicesResponse = pdfServices.getJobResult(location, CreatePDFResult.class);
30
31 // Get content from the resulting asset(s)
32 Asset resultAsset = pdfServicesResponse.getResult().getAsset();
33 StreamAsset streamAsset = pdfServices.getContent(resultAsset);
34
35 // Creates an output stream and copy stream asset's content to it
36 File.createDirectories(Paths.get("output/"));
37 OutputStream outputStream = Files.newOutputStream(new File("output/createPDFFromDOCX.pdf").toPath());
38 LOGGER.info("Saving asset at output/createPDFFromDOCX.pdf");
39 IOUtils.copy(streamAsset.getInputStream(), outputStream);
40 outputStream.close();
41 } catch (ServiceApiException | IOException | SDKException | ServiceUsageException ex) {
42 LOGGER.error("Exception encountered while executing the operation", ex);
43 }
44 }
45}

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
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 (InputStream inputStream = Files.newInputStream(new File("src/main/resources/createPDFInput.docx").toPath())) {
13 // Initial setup, create credentials instance
14 Credentials credentials = new ServicePrincipalCredentials(
15 System.getenv("PDF_SERVICES_CLIENT_ID"),
16 System.getenv("PDF_SERVICES_CLIENT_SECRET"));
17
18 // Creates a PDF Services instance
19 PDFServices pdfServices = new PDFServices(credentials);
20
21 // Creates an asset(s) from source file(s) and upload
22 Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.DOCX.getMediaType());
23
24 // Create parameters for the job
25 CreatePDFParams createPDFWordParams = CreatePDFParams.wordParamsBuilder().
26 withDocumentLanguage(DocumentLanguage.EN_US).
27 build();
28
29 // Creates a new job instance
30 CreatePDFJob createPDFJob = new CreatePDFJob(asset)
31 .setParams(createPDFWordParams);
32
33 // Submit the job and gets the job result
34 String location = pdfServices.submit(createPDFJob);
35 PDFServicesResponse<CreatePDFResult> pdfServicesResponse = pdfServices.getJobResult(location, CreatePDFResult.class);
36
37 // Get content from the resulting asset(s)
38 Asset resultAsset = pdfServicesResponse.getResult().getAsset();
39 StreamAsset streamAsset = pdfServices.getContent(resultAsset);
40
41 // Creates an output stream and copy stream asset's content to it
42 File.createDirectories(Paths.get("output/"));
43 OutputStream outputStream = Files.newOutputStream(new File("output/createPDFFromDOCXWithOptionsOutput.pdf").toPath());
44 LOGGER.info("Saving asset at output/createPDFFromDOCXWithOptionsOutput.pdf");
45 IOUtils.copy(streamAsset.getInputStream(), outputStream);
46 outputStream.close();
47 } catch (ServiceApiException | IOException | SDKException | ServiceUsageException ex) {
48 LOGGER.error("Exception encountered while executing operation", ex);
49 }
50 }
51 }

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
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.htmltopdf.StaticHTMLToPDF
4
5public class StaticHTMLToPDF {
6
7 // Initialize the logger.
8 private static final Logger LOGGER = LoggerFactory.getLogger(StaticHTMLToPDF.class);
9
10 public static void main(String[] args) {
11
12 try (InputStream inputStream = Files.newInputStream(new File("src/main/resources/createPDFFromStaticHtmlInput.zip").toPath())) {
13 // Initial setup, create credentials instance
14 Credentials credentials = new ServicePrincipalCredentials(
15 System.getenv("PDF_SERVICES_CLIENT_ID"),
16 System.getenv("PDF_SERVICES_CLIENT_SECRET"));
17
18 // Creates a PDF Services instance
19 PDFServices pdfServices = new PDFServices(credentials);
20
21 // Creates an asset(s) from source file(s) and upload
22 Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.ZIP.getMediaType());
23
24 // Create parameters for the job
25 HTMLToPDFParams htmlToPDFParams = getHTMLToPDFParams();
26
27 // Creates a new job instance
28 HTMLToPDFJob htmLtoPDFJob = new HTMLToPDFJob(asset)
29 .setParams(htmlToPDFParams);
30
31 // Submit the job and gets the job result
32 String location = pdfServices.submit(htmLtoPDFJob);
33 PDFServicesResponse<HTMLToPDFResult> pdfServicesResponse = pdfServices.getJobResult(location, HTMLToPDFResult.class);
34
35 // Get content from the resulting asset(s)
36 Asset resultAsset = pdfServicesResponse.getResult().getAsset();
37 StreamAsset streamAsset = pdfServices.getContent(resultAsset);
38
39 // Creates an output stream and copy stream asset's content to it
40 File.createDirectories(Paths.get("output/"));
41 OutputStream outputStream = Files.newOutputStream(new File("output/staticHTMLToPDFOutput.pdf").toPath());
42 LOGGER.info("Saving asset at output/staticHTMLToPDFOutput.pdf");
43 IOUtils.copy(streamAsset.getInputStream(), outputStream);
44 outputStream.close();
45 } catch (ServiceApiException | IOException | SDKException | ServiceUsageException ex) {
46 LOGGER.error("Exception encountered while executing operation", ex);
47 }
48 }
49 private static HTMLToPDFParams getHTMLToPDFParams() {
50 // Define the page layout, in this case an 8 x 11.5 inch page (effectively portrait orientation)
51 PageLayout pageLayout = new PageLayout();
52 pageLayout.setPageSize(8, 11.5);
53
54 return new HTMLToPDFParams.Builder()
55 .includeHeaderFooter(true).withPageLayout(pageLayout)
56 .build();
57 }
58}

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
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.htmltopdf.HTMLWithInlineCSSToPDF
4
5 public class HTMLWithInlineCSSToPDF {
6
7 // Initialize the logger.
8 private static final Logger LOGGER = LoggerFactory.getLogger(HTMLWithInlineCSSToPDF.class);
9
10 public static void main(String[] args) {
11 try (InputStream inputStream = Files.newInputStream(new File("src/main/resources/createPDFFromHTMLWithInlineCSSInput.html").toPath())) {
12 // Initial setup, create credentials instance
13 Credentials credentials = new ServicePrincipalCredentials(
14 System.getenv("PDF_SERVICES_CLIENT_ID"),
15 System.getenv("PDF_SERVICES_CLIENT_SECRET"));
16
17 // Creates a PDF Services instance
18 PDFServices pdfServices = new PDFServices(credentials);
19
20 // Creates an asset(s) from source file(s) and upload
21 Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.HTML.getMediaType());
22
23 // Create parameters for the job
24 HTMLToPDFParams htmlToPDFParams = getHTMLToPDFParams();
25
26 // Creates a new job instance
27 HTMLToPDFJob htmLtoPDFJob = new HTMLToPDFJob(asset)
28 .setParams(htmlToPDFParams);
29
30 // Submit the job and gets the job result
31 String location = pdfServices.submit(htmLtoPDFJob);
32 PDFServicesResponse<HTMLToPDFResult> pdfServicesResponse = pdfServices.getJobResult(location, HTMLToPDFResult.class);
33
34 // Get content from the resulting asset(s)
35 Asset resultAsset = pdfServicesResponse.getResult().getAsset();
36 StreamAsset streamAsset = pdfServices.getContent(resultAsset);
37
38 // Creates an output stream and copy stream asset's content to it
39 File.createDirectories(Paths.get("output/"));
40 OutputStream outputStream = Files.newOutputStream(new File("output/htmlWithInlineCSSToPDFOutput.pdf").toPath());
41 LOGGER.info("Saving asset at output/htmlWithInlineCSSToPDFOutput.pdf");
42 IOUtils.copy(streamAsset.getInputStream(), outputStream);
43 outputStream.close();
44 } catch (ServiceApiException | IOException | SDKException | ServiceUsageException ex) {
45 LOGGER.error("Exception encountered while executing operation", ex);
46 }
47 }
48
49 private static HTMLToPDFParams getHTMLToPDFParams() {
50 // Define the page layout, in this case an 8 x 11.5 inch page (effectively portrait orientation)
51 PageLayout pageLayout = new PageLayout();
52 pageLayout.setPageSize(20, 25);
53
54 return new HTMLToPDFParams.Builder()
55 .includeHeaderFooter(true).withPageLayout(pageLayout)
56 .build();
57 }
58}

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
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.htmltopdf.HTMLToPDFFromURL
4
5 public class HTMLToPDFFromURL {
6
7 // Initialize the logger.
8 private static final Logger LOGGER = LoggerFactory.getLogger(HTMLToPDFFromURL.class);
9
10 public static void main(String[] args) {
11
12 try {
13 // Initial setup, create credentials instance
14 Credentials credentials = new ServicePrincipalCredentials(
15 System.getenv("PDF_SERVICES_CLIENT_ID"),
16 System.getenv("PDF_SERVICES_CLIENT_SECRET"));
17
18 // Creates a PDF Services instance
19 PDFServices pdfServices = new PDFServices(credentials);
20
21 String htmlURL = "<HTML URL>";
22
23 // Create parameters for the job
24 HTMLToPDFParams htmlToPDFParams = getHTMLToPDFParams();
25
26 // Creates a new job instance
27 HTMLToPDFJob htmLtoPDFJob = new HTMLToPDFJob(htmlURL)
28 .setParams(htmlToPDFParams);
29
30 // Submit the job and gets the job result
31 String location = pdfServices.submit(htmLtoPDFJob);
32 PDFServicesResponse<HTMLToPDFResult> pdfServicesResponse = pdfServices.getJobResult(location, HTMLToPDFResult.class);
33
34 // Get content from the resulting asset(s)
35 Asset resultAsset = pdfServicesResponse.getResult().getAsset();
36 StreamAsset streamAsset = pdfServices.getContent(resultAsset);
37
38 // Creates an output stream and copy stream asset's content to it
39 File.createDirectories(Paths.get("output/"));
40 OutputStream outputStream = Files.newOutputStream(new File("output/htmlToPDFFromURLOutput.pdf").toPath());
41 LOGGER.info("Saving asset at output/htmlToPDFFromURLOutput.pdf");
42 IOUtils.copy(streamAsset.getInputStream(), outputStream);
43 } catch (ServiceApiException | IOException | SDKException | ServiceUsageException ex) {
44 LOGGER.error("Exception encountered while executing operation", ex);
45 }
46 }
47
48 private static HTMLToPDFParams getHTMLToPDFParams() {
49 // Define the page layout, in this case an 8 x 11.5 inch page (effectively portrait orientation)
50 PageLayout pageLayout = new PageLayout();
51 pageLayout.setPageSize(20, 25);
52
53 return new HTMLToPDFParams.Builder()
54 .includeHeaderFooter(true).withPageLayout(pageLayout)
55 .build();
56 }
57}

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
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.htmltopdf.DynamicHTMLToPDF
4 public class DynamicHTMLToPDF {
5
6 // Initialize the logger.
7 private static final Logger LOGGER = LoggerFactory.getLogger(DynamicHTMLToPDF.class);
8
9 public static void main(String[] args) {
10
11 try (InputStream inputStream = Files.newInputStream(new File("src/main/resources/createPDFFromDynamicHtmlInput.zip").toPath())) {
12 // Initial setup, create credentials instance
13 Credentials credentials = new ServicePrincipalCredentials(
14 System.getenv("PDF_SERVICES_CLIENT_ID"),
15 System.getenv("PDF_SERVICES_CLIENT_SECRET"));
16
17 // Creates a PDF Services instance
18 PDFServices pdfServices = new PDFServices(credentials);
19
20 // Creates an asset(s) from source file(s) and upload
21 Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.ZIP.getMediaType());
22
23 // Create parameters for the job
24 HTMLToPDFParams htmlToPDFParams = getHTMLToPDFParams();
25
26 // Creates a new job instance
27 HTMLToPDFJob htmLtoPDFJob = new HTMLToPDFJob(asset)
28 .setParams(htmlToPDFParams);
29
30 // Submit the job and gets the job result
31 String location = pdfServices.submit(htmLtoPDFJob);
32 PDFServicesResponse<HTMLToPDFResult> pdfServicesResponse = pdfServices.getJobResult(location, HTMLToPDFResult.class);
33
34 // Get content from the resulting asset(s)
35 Asset resultAsset = pdfServicesResponse.getResult().getAsset();
36 StreamAsset streamAsset = pdfServices.getContent(resultAsset);
37
38 // Creates an output stream and copy stream asset's content to it
39 File.createDirectories(Paths.get("output/"));
40 OutputStream outputStream = Files.newOutputStream(new File("output/dynamicHTMLToPDFOutput.pdf").toPath());
41 LOGGER.info("Saving asset at output/dynamicHTMLToPDFOutput.pdf");
42 IOUtils.copy(streamAsset.getInputStream(), outputStream);
43 outputStream.close();
44 } catch (ServiceApiException | IOException | SDKException | ServiceUsageException ex) {
45 LOGGER.error("Exception encountered while executing operation", ex);
46 }
47 }
48
49 private static HTMLToPDFParams getHTMLToPDFParams() {
50 // Define the page layout, in this case an 8 x 11.5 inch page (effectively portrait orientation)
51 PageLayout pageLayout = new PageLayout();
52 pageLayout.setPageSize(8, 11.5);
53
54 // Sets the dataToMerge field that needs to be populated in the HTML before its conversion
55 JSONObject dataToMerge = new JSONObject();
56 dataToMerge.put("title","Create, Convert PDFs and More!");
57 dataToMerge.put("sub_title","Easily integrate PDF actions within your document workflows.");
58
59 return new HTMLToPDFParams.Builder()
60 .includeHeaderFooter(true).withPageLayout(pageLayout).withDataToMerge(dataToMerge)
61 .build();
62 }
63}
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.