com.dynamicpdf.api.Pdf Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dynamicpdf-api Show documentation
Show all versions of dynamicpdf-api Show documentation
A Java Client API that uses the DynamicPDF API to create, merge, split, form fill, stamp, secure/encrypt PDF documents.
package com.dynamicpdf.api;
import java.util.HashSet;
import java.util.List;
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";}
/**
* 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 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;
}
/**
* 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 getTemplates()
{
return this.instructions.getTemplates();
}
/**
* Gets the fonts.
* @return The fonts.
*/
public HashSet getFonts()
{
return this.instructions.getFonts();
}
/**
* Gets the formFields.
* @return The formFields.
*/
public List getFormFields()
{
return this.instructions.getFormFields();
}
/**
* Gets the outlines.
* @return The outlines.
*/
@JsonIgnore
public OutlineList getOutlines()
{
return this.instructions.getOutlines();
}
/*
* Gets the instructions json based on the inputs passed.
* @return The json string.
*/
public String getInstructionsJson() {
for (Input input : instructions.getInputs()) {
if (input.getType() == InputType.PAGE) {
PageInput pageInput = (PageInput)input;
for (Element element : pageInput.getElements()) {
if (element.getTextFont() != null) {
instructions.getFonts().add(element.getTextFont());
}
}
}
if (input.getTemplate() != null) {
instructions.getTemplates().add(input.getTemplate());
if (input.getTemplate().getElements() != null && input.getTemplate().getElements().size() > 0) {
for (Element element : input.getTemplate().getElements()) {
if (element.getTextFont() != null) {
instructions.getFonts().add(element.getTextFont());
}
}
}
}
}
String jsonText = null;
ObjectMapper basicMapper = new ObjectMapper();
try {
jsonText = basicMapper.writeValueAsString(this.instructions);
} catch(JsonProcessingException e1) {
e1.printStackTrace();
}
return jsonText;
}
/**
* Process to create pdf.
* @return Pdf Response
*/
public PdfResponse process()
{
CompletableFuture cf = processAsync();
try {
return cf.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return null;
}
/**
* Process to create pdf.
*
* @return collection of PdfResponse
as multithreading tasks CompletableFuture
.
*/
public CompletableFuture processAsync()
{
return CompletableFuture.supplyAsync(() -> {
RequestSpecification spec = super.createRequestSpecification();
PdfResponse pdfResponse = null;
HashSet finalResources = new HashSet();
String jsonText = null;
ObjectMapper basicMapper = new ObjectMapper();
for (Input input : instructions.getInputs()) {
if (input.getType() == InputType.PAGE) {
PageInput pageInput = (PageInput) input;
for (Element element : pageInput.getElements()) {
if (element.getResource() != null) {
finalResources.add(element.getResource());
}
if (element.getTextFont() != null) {
instructions.getFonts().add(element.getTextFont());
}
}
}
for (Resource resource : input.getResources()) {
finalResources.add(resource);
}
if (input.getTemplate() != null) {
instructions.getTemplates().add(input.getTemplate());
if (input.getTemplate().getElements() != null && input.getTemplate().getElements().size() > 0) {
for (Element element : input.getTemplate().getElements()) {
if (element.getResource() != null) {
finalResources.add(element.getResource());
}
if (element.getTextFont() != null) {
instructions.getFonts().add(element.getTextFont());
}
}
}
}
}
try {
jsonText = basicMapper.writeValueAsString(this.instructions);
} catch (JsonProcessingException e1) {
e1.printStackTrace();
}
spec.multiPart("Instructions", "Instructions.json", jsonText.getBytes(), "application/json");
if (instructions.getInputs() == null) {
throw new EndpointException("Minimum one input required.");
}
for (Resource resource : finalResources) {
if (resource.getType() == ResourceType.LAYOUTDATA) {
LayoutDataResource res = (LayoutDataResource) resource;
spec.multiPart("Resource", res.getLayoutDataResourceName(), resource.getData(), resource.getMimeType());
} else {
spec.multiPart("Resource", resource.getResourceName(), resource.getData(), resource.getMimeType());
}
}
Response response = RestAssured
.given()
.header("Accept", "*/*")
.header("Content-Type","multipart/form-data")
.spec(spec)
.post(getDefaultBaseUrl() + "/"+ getEndpointVersion() + "/" + getEndpointName());
if (response.getStatusCode() == 200)
{
pdfResponse = new PdfResponse(response.asByteArray());
pdfResponse.setIsSuccessful(true);
pdfResponse.setStatusCode(response.getStatusCode());
}
else
{
pdfResponse = new PdfResponse();
pdfResponse.setErrorJson(response.asString());
pdfResponse.setIsSuccessful(false);
pdfResponse.setStatusCode(response.getStatusCode());
}
return pdfResponse;
});
}
}