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

com.dynamicpdf.api.Pdf Maven / Gradle / Ivy

Go to download

A Java Client API that uses the DynamicPDF API to create, merge, split, form fill, stamp, secure/encrypt PDF documents.

There is a newer version: 1.10.1
Show newest version

package com.dynamicpdf.api;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import com.dynamicpdf.api.elements.Element;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

/**
 * Represents a pdf endpoint.
 */

public class Pdf extends Endpoint
{
	PdfInstructions instructions;
	private HashSet resources = new HashSet();

	/**
	 * Initializes a new instance of the Pdf class.
	 */

	public Pdf() { 
		super();        
		this.instructions = new PdfInstructions();
	}

	/**
	 * Gets the collection of resource.
	 * @return The collection of resource.
	 */
	String getEndpointName() { return "pdf";}

	/**
     * Adds additional resource to the endpoint.
     * @param resourcePath The resource file path.
     * @param resourceName The name of the resource.
     */
    public void addAdditionalResource(String resourcePath, String resourceName)
    {
        if( resourceName == null) {
        	Path path = Paths.get(resourcePath);
            Path resourceFileName = path.getFileName();
            resourceName = resourceFileName.toString();
        }
        AdditionalResource resource = new AdditionalResource( resourcePath, resourceName);
        getResources().add(resource);
    }
    
    /**
     * Adds additional resource to the endpoint.
     * @param resourcePath The resource file path.
     */
    public void addAdditionalResource(String resourcePath)
    {
    	Path path = Paths.get(resourcePath);
        Path resourceFileName = path.getFileName();
        String resourceName = resourceFileName.toString();
        AdditionalResource resource = new AdditionalResource( resourcePath, resourceName);
        getResources().add(resource);
    }

    /**
     * Adds additional resource to the endpoint.
     * @param resourceData The resource data.
     * @param additionalResourceType The type of the resource.
     * @param resourceName The name of the resource.
     */
    public void addAdditionalResource(byte[] resourceData,AdditionalResourceType additionalResourceType, String resourceName)
    {
    	ResourceType type = ResourceType.PDF;
        switch(additionalResourceType)
        {
            case FONT:
                type = ResourceType.FONT;
                break;
            case IMAGE:
                type = ResourceType.IMAGE;
                break;
            case PDF:
                type = ResourceType.PDF;
                break;
            case HTML:
                type = ResourceType.HTML;
                break;
        }
        AdditionalResource resource = new AdditionalResource(resourceData, resourceName, type);
        getResources().add(resource);
    }
    
    
	/**
	 * Sets the collection of resource.
	 * @return The collection of resource.
	 */
	public HashSet getResources() { return resources; }

	/**
	 * Gets the author.
	 * @return The author.
	 */
	public String getAuthor()
	{
		return this.instructions.getAuthor();            
	}

	/**
	 * Sets the author.
	 * @param value The author.
	 */
	public void setAuthor(String value)
	{
		this.instructions.setAuthor(value);
	}

	/**
	 * Gets the title.
	 * @return The title.
	 */
	public String getTitle()
	{
		return this.instructions.getTitle();
	}

	/**
	 * Sets the title.
	 * @param value The title.
	 */
	public void setTitle(String value) 
	{
		this.instructions.setTitle(value);
	}

	/**
	 * Gets the subject.
	 * @return The subject.
	 */
	public String getSubject()
	{ 
		return this.instructions.getSubject();
	}

	/**
	 * Sets the subject.
	 * @param value The subject.
	 */
	public void setSubject(String value) 
	{ 
		this.instructions.setSubject(value);
	}

	/**
	 * Gets the creator.
	 * @return The creator.
	 */
	public String getCreator()
	{ 
		return this.instructions.getCreator();
	}

	/**
	 * Sets the creator.
	 * @param value The creator.
	 */
	public void setCreator(String value) 
	{ 
		this.instructions.setCreator(value);
	}

	/**
	 * Gets the producer.
	 * @return The producer.
	 */
	public String getProducer()
	{ 
		return this.instructions.getProducer();
	}

	/**
	 * Sets the producer.
	 * @param value The producer.
	 */
	public void setProducer(String value) 
	{ 
		this.instructions.setProducer(value);
	}

	/**
	 * Gets the keywords.
	 * @return The keywords.
	 */
	public String getKeywords() 
	{
		return this.instructions.getKeywords();
	}

	/**
	 * Sets the keywords.
	 * @param value The keywords.
	 */
	public void setKeywords(String value) 
	{ 

		this.instructions.setKeywords(value);
	}

	/**
	 * Gets the security.
	 * @return The security.
	 */
	public Security getSecurity() 
	{
		return this.instructions.getSecurity();
	}

	/**
	 * Sets the security.
	 * @param value The security.
	 */
	public void setSecurity(Security value) 
	{ 
		this.instructions.setSecurity(value);
	}

	/**
	 * Gets the value indicating whether to flatten all form fields.
	 * @return The value indicating whether to flatten all form fields.
	 */
	public boolean getFlattenAllFormFields()
	{
		return this.instructions.getFlattenAllFormFields();
	}

	/**
	 * Sets the value indicating whether to flatten all form fields.
	 * @param value The value indicating whether to flatten all form fields.
	 */
	public void setFlattenAllFormFields(boolean value)
	{ 
		this.instructions.setFlattenAllFormFields(value);
	}

	/**
	 * Gets the value indicating whether to retain signature form field.
	 * @return The value indicating whether to retain signature form field.
	 */
	public boolean getRetainSignatureFormFields() 
	{
		return this.instructions.getRetainSignatureFormFields();            
	}

	/**
	 * Sets the value indicating whether to retain signature form field.
	 * @param value The value indicating whether to retain signature form field.
	 */
	public void setRetainSignatureFormFields(boolean value) 
	{
		this.instructions.setRetainSignatureFormFields(value);
	}

	/**
	 * Returns a PdfInput object containing the input pdf.
	 * @param resource The resource of type PdfResource.
	 * @param options The merge options for the pdf.
	 * @return Added pdf pages
	 */

	public PdfInput addPdf(PdfResource resource, MergeOptions options)
	{
		PdfInput input = new PdfInput(resource, options);
		this.getInputs().add(input);
		return input;
	}

	/**
	 * 
	 * @param resource The resource of type PdfResource.
	 * @return Added pdf pages
	 */
	public PdfInput addPdf(PdfResource resource)
	{
		return addPdf(resource, null);
	}

	/**
	 * Returns a PdfInput object containing the input pdf.
	 * @param cloudResourcePath The resource path in cloud resource manager.
	 * @param options The merge options for the pdf.
	 * @return Added pdf pages
	 */
	public PdfInput addPdf(String cloudResourcePath, MergeOptions options)
	{
		PdfInput input = new PdfInput(cloudResourcePath, options);
		this.getInputs().add(input);
		return input;
	}

	/**
	 * 
	 * @param cloudResourcePath The resource path in cloud resource manager.
	 * @return Added pdf pages
	 */
	public PdfInput addPdf(String cloudResourcePath)
	{            
		return addPdf(cloudResourcePath, null);
	}

	/**
	 * Returns an ImageInput object containing the input pdf.
	 * @param resource The resource of type ImageResource.
	 * @return Added pdf pages
	 */

	public ImageInput addImage(ImageResource resource)
	{
		ImageInput input = new ImageInput(resource);
		this.getInputs().add(input);
		return input;
	}

	/**
	 * Returns an ImageInput object containing the input pdf.
	 * @param cloudResourcePath The resource path in cloud resource manager.
	 * @return Added pdf pages
	 */

	public ImageInput addImage(String cloudResourcePath)
	{
		ImageInput input = new ImageInput(cloudResourcePath);
		this.getInputs().add(input);
		return input;
	}

	/**
	 * @deprecated This method is deprecated, use {@link #addHtml(HtmlResource, String, PageSize, PageOrientation, Float)} method instead.
	 * Returns an HtmlInput object containing the input html.
	 * @param resource The resource of type HtmlResource.
	 * @param basepath The BasePath option.
	 * @param size The page dimensions.
	 * @param orientation The orientation of the page.
	 * @param margins The margins on the page.
	 * @return Added pdf pages
	 */
	@Deprecated
    public HtmlInput AddHtml(HtmlResource resource, String basepath, PageSize size, PageOrientation orientation, Float margins)
    {
		return addHtml(resource, basepath, size, orientation, margins);
    }

    /**
	 * @deprecated This method is deprecated, use {@link #addHtml(String, String, PageSize, PageOrientation, Float)} method instead.
	 * Returns an HtmlInput object containing the input html.
	 * @param html The HTML input string.
	 * @param basepath The BasePath option.
	 * @param size The page dimensions.
	 * @param orientation The orientation of the page.
	 * @param margins The margins on the page.
	 * @return Added pdf pages
	 */
	@Deprecated
    public HtmlInput AddHtml(String html, String basepath, PageSize size, PageOrientation orientation, Float margins)
    {
    	return addHtml(new HtmlResource(html), basepath, size, orientation, margins);
    }

	/**
	 * Returns an HtmlInput object containing the input html.
	 * @param resource The resource of type HtmlResource.
	 * @param basepath The BasePath option.
	 * @param size The page dimensions.
	 * @param orientation The orientation of the page.
	 * @param margins The margins on the page.
	 * @return Added pdf pages
	 */
    public HtmlInput addHtml(HtmlResource resource, String basepath, PageSize size, PageOrientation orientation, Float margins)
    {
        HtmlInput input = new HtmlInput(resource, basepath, size, orientation, margins);
        this.getInputs().add(input);
        return input;
    }

    /**
	 * Returns an HtmlInput object containing the input html.
	 * @param html The HTML input string.
	 * @param basepath The BasePath option.
	 * @param size The page dimensions.
	 * @param orientation The orientation of the page.
	 * @param margins The margins on the page.
	 * @return Added pdf pages
	 */
    public HtmlInput addHtml(String html, String basepath, PageSize size, PageOrientation orientation, Float margins)
    {
    	return addHtml(new HtmlResource(html), basepath, size, orientation, margins);
    }
    
    /**
     * Returns a WordInput object containing the input pdf.
     * @param resource The resource of type WordResource.
     * @param size The page dimensions.
     * @param orientation The orientation of the page.
     * @param margins The margins on the page.
     * @return Added pdf pages
     */
	public WordInput addWord(WordResource resource, PageSize size, PageOrientation orientation, Float margins) {
		WordInput input = new WordInput(resource, size, orientation, margins);
		this.getInputs().add(input);
		return input;
	}
    
	/**
	 * Returns a DlexInput object containing the input pdf.
	 * @param dlexResource The dlex resource of type DlexResource.
	 * @param layoutData The layout data resource of type LayoutDataResource.
	 * @return Added pdf pages
	 */

	public DlexInput addDlex(DlexResource dlexResource, LayoutDataResource layoutData)
	{
		DlexInput input = new DlexInput(dlexResource, layoutData);
		this.getInputs().add(input);
		return input;
	}

	/**
	 * Returns a DlexInput object containing the input pdf.
	 * @param cloudResourcePath The resource path in cloud resource manager.
	 * @param layoutData The layout data resource of type LayoutDataResource.
	 * @return Added pdf pages
	 */

	public DlexInput addDlex(String cloudResourcePath, LayoutDataResource layoutData)
	{
		DlexInput input = new DlexInput(cloudResourcePath, layoutData);
		this.getInputs().add(input);
		return input;
	}

	/**
	 * Returns a DlexInput object containing the input pdf.
	 * @param cloudResourcePath The resource path in cloud resource manager.
	 * @param layoutData The json data string used to create the PDF report.
	 * @return Added pdf pages
	 */

	public DlexInput addDlex(String cloudResourcePath, String layoutData)
	{
		DlexInput input = new DlexInput(cloudResourcePath, layoutData);
		this.getInputs().add(input);
		return input;
	}
	
	/**
	 * Returns a DlexInput object containing the input pdf.
	 * @param dlexResource The resource path in cloud resource manager.
	 * @param layoutData The json data string used to create the PDF report.
	 * @return Added pdf pages
	 */
	public DlexInput addDlex(DlexResource dlexResource, String layoutData) {
		DlexInput input = new DlexInput(dlexResource, layoutData);
		this.getInputs().add(input);
		return input;
	}

	/**
	 * Returns a PageInput object containing the input pdf.
	 * @param pageWidth The width of the page.
	 * @param pageHeight The height of the page.
	 * @return Added pdf pages
	 */

	public PageInput addPage(float pageWidth, float pageHeight)
	{
		PageInput input = new PageInput(pageWidth, pageHeight);
		this.getInputs().add(input);
		return input;
	}

	/**
	 * Returns a PageInput object containing the input pdf.
	 * @return A PageInput object containing the input pdf.
	 */
	public PageInput addPage()
	{
		PageInput input = new PageInput();
		this.getInputs().add(input);
		return input;
	}

	/**
	 * Gets the inputs.
	 * @return The inputs.
	 */
	public List getInputs()
	{
		return this.instructions.getInputs();
	}

	/**
	 * Gets the templates.
	 * @return The templates.
	 */
	public HashSet