All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adobe.pdfservices.operation.pdfjobs.jobs.HTMLToPDFJob Maven / Gradle / Ivy

Go to download

Adobe PDF Services SDK allows you to access RESTful APIs to create, convert, and manipulate PDFs within your applications. Older versions can be found under groupId: com.adobe.documentservices, artifactId: pdftools-sdk

There is a newer version: 4.2.0
Show newest version
/*
 * Copyright 2024 Adobe
 * All Rights Reserved.
 *
 * NOTICE: Adobe permits you to use, modify, and distribute this file in
 * accordance with the terms of the Adobe license agreement accompanying
 * it. If you have received this file from a source other than Adobe,
 * then your use, modification, or distribution of it requires the prior
 * written permission of Adobe.
 */

package com.adobe.pdfservices.operation.pdfjobs.jobs;

import com.adobe.pdfservices.operation.PDFServicesJob;
import com.adobe.pdfservices.operation.config.notifier.NotifierConfig;
import com.adobe.pdfservices.operation.exception.ServiceApiException;
import com.adobe.pdfservices.operation.internal.ExecutionContext;
import com.adobe.pdfservices.operation.internal.PDFServicesHelper;
import com.adobe.pdfservices.operation.internal.constants.CustomErrorMessages;
import com.adobe.pdfservices.operation.internal.constants.OperationHeaderInfoEndpointMap;
import com.adobe.pdfservices.operation.internal.dto.request.PlatformApiRequest;
import com.adobe.pdfservices.operation.internal.dto.request.htmltopdf.HTMLToPDFExternalAssetRequest;
import com.adobe.pdfservices.operation.internal.dto.request.htmltopdf.HTMLToPDFInternalAssetRequest;
import com.adobe.pdfservices.operation.internal.http.DefaultRequestHeaders;
import com.adobe.pdfservices.operation.internal.http.HttpResponse;
import com.adobe.pdfservices.operation.internal.util.ObjectUtil;
import com.adobe.pdfservices.operation.io.Asset;
import com.adobe.pdfservices.operation.io.CloudAsset;
import com.adobe.pdfservices.operation.io.ExternalAsset;
import com.adobe.pdfservices.operation.pdfjobs.params.htmltopdf.HTMLToPDFParams;

import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * A job that converts a HTML file to a PDF file. Some source formats may have associated conversion parameters
 * which can be set in the {@link HTMLToPDFJob#setParams(HTMLToPDFParams)} method.
 *
 *
 * 

* An HTML input can be provided either as a local zip archive or as a static HTML file with inline CSS. * Alternatively, an HTML can also be specified via URL. *
* While creating the corresponding Asset instance, the media type must be: *

    *
  • "application/zip", if the input is a local zip archive.
  • *
  • "text/html", if the input is a static HTML file with inline CSS
  • *
*
* In case the input is a local zip archive, it must have the following structure: *
    *
  • The main HTML file must be named "index.html".
  • *
  • "index.html" must exist at the top level of zip archive, not in a folder.
  • *
*

* Sample layout:

 * html_files.zip
 * |__index.html
 * |__referenced_file_1.css
 * |__referenced_file_2.jpeg
 * |__subfolder_1
 * |_____referenced_file_3.jpeg
 * 
* *

* Sample Usage: *

{@code
 *             InputStream inputStream = new FileInputStream(new File("SOURCE_PATH"));
 *
 *             Credentials credentials = new ServicePrincipalCredentials(
 *                     System.getenv("PDF_SERVICES_CLIENT_ID"),
 *                     System.getenv("PDF_SERVICES_CLIENT_SECRET"));
 *
 *             PDFServices pdfServices = new PDFServices(credentials);
 *
 *             HTMLToPDFJob htmLtoPDFJob = new HTMLToPDFJob("URL");
 *
 *             String location = pdfServices.submit(htmLtoPDFJob);
 *             PDFServicesResponse pdfServicesResponse = pdfServices.getJobResult(location, HTMLtoPDFResult.class);
 *
 *             Asset resultAsset = pdfServicesResponse.getResult().getAsset();
 *             StreamAsset streamAsset = pdfServices.getContent(resultAsset);
 * }
**/ public class HTMLToPDFJob extends PDFServicesJob { private Asset inputAsset; private String inputURL; private Asset outputAsset; private HTMLToPDFParams htmLtoPDFParams; /** * Constructs a new {@code HTMLToPDFJob} instance. * * @param asset {@link Asset} object containing the input file; can not be null. */ public HTMLToPDFJob(Asset asset) { ObjectUtil.requireNonNull(asset, String.format(CustomErrorMessages.GENERIC_CAN_NOT_BE_NULL, "Input Asset")); this.inputAsset = asset; } /** * Constructs a new {@code HTMLToPDFJob} instance. * * @param inputURL string representing the input URL; can not be null. */ public HTMLToPDFJob(String inputURL) { ObjectUtil.requireNonNull(inputURL, String.format(CustomErrorMessages.GENERIC_CAN_NOT_BE_NULL, "Input URL")); this.inputURL = inputURL; } @Override protected String process(ExecutionContext executionContext) throws ServiceApiException { return this.process(executionContext, null); } @Override protected String process(ExecutionContext executionContext, List notifyConfigList) throws ServiceApiException { this.validate(executionContext); PlatformApiRequest htmlToPDFRequest = generatePlatformApiRequest(notifyConfigList); String xRequestId = UUID.randomUUID().toString(); HttpResponse response = PDFServicesHelper.submitJob(executionContext, htmlToPDFRequest, xRequestId, OperationHeaderInfoEndpointMap.HTML_TO_PDF); return response.getHeaders().get(DefaultRequestHeaders.LOCATION_HEADER_NAME); } /** * Sets the HTML to PDF conversion parameters. * * @param htmLtoPDFParams {@link HTMLToPDFParams}; can not be null. * @return {@code HTMLToPDFJob} instance */ public HTMLToPDFJob setParams(HTMLToPDFParams htmLtoPDFParams) { ObjectUtil.requireNonNull(htmLtoPDFParams, String.format(CustomErrorMessages.GENERIC_CAN_NOT_BE_NULL, "HTML " + "To PDF parameters")); this.htmLtoPDFParams = htmLtoPDFParams; return this; } /** * Sets the output asset for the job. * {@code @note} External assets can be set as output only when input is external asset as well * * @param asset {@link Asset} object representing the output asset; can not be null. * @return {@code HTMLToPDFJob} instance */ public HTMLToPDFJob setOutput(Asset asset) { ObjectUtil.requireNonNull(asset, String.format(CustomErrorMessages.GENERIC_CAN_NOT_BE_NULL, "Output asset")); if (this.inputAsset instanceof CloudAsset) { throw new IllegalArgumentException(CustomErrorMessages.SET_OUTPUT_VALIDATE); } this.outputAsset = asset; return this; } private PlatformApiRequest generatePlatformApiRequest(List notifyConfigList) { PlatformApiRequest htmlToPDFRequest; if (Objects.nonNull(this.inputAsset)) { if (this.inputAsset instanceof CloudAsset) { htmlToPDFRequest = new HTMLToPDFInternalAssetRequest(( (CloudAsset) this.inputAsset ).getAssetId(), null, this.htmLtoPDFParams, notifyConfigList); } else { htmlToPDFRequest = new HTMLToPDFExternalAssetRequest((ExternalAsset) this.inputAsset, this.htmLtoPDFParams, notifyConfigList).setOutput((ExternalAsset) outputAsset); } } else { htmlToPDFRequest = new HTMLToPDFInternalAssetRequest(null, this.inputURL, this.htmLtoPDFParams, notifyConfigList); } return htmlToPDFRequest; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy