Make the POST call
Last update: May 14, 2024
- Topics:
- Output Service
CREATED FOR:
- Beginner
- Intermediate
- Developer
The next step is to make an HTTP POST call to the endpoint with the necessary parameters. The template and the datafiles are provided as resource files. Properties of the generated pdf are specified via the option’s parameter in the request.The property embedFonts is used to embed custom fonts in the generated pdf.Please follow this documentation to deploy custom fonts to your Forms cloud instance. The properties are specified in the options.json resource file. Since, the end point has token based authentication we pass the Access Token in the request header.
The following code was used to generate pdf by merging data with the template
public class DocumentGeneration
{
public String SAVE_LOCATION = "c:\\aspire1";
public void mergeDataWithXdpTemplate(String postURL)
{
HttpPost httpPost = new HttpPost(postURL);
CredentialUtilites cu = new CredentialUtilites();
String accessToken = cu.getAccessToken();
httpPost.addHeader("Authorization", "Bearer " + accessToken);
ClassLoader classLoader = DocumentGeneration.class.getClassLoader();
URL templateFile = classLoader.getResource("templates/custom_fonts.xdp");
File xdpTemplate = new File(templateFile.getPath());
URL url = classLoader.getResource("datafiles");
System.out.println(url.getPath());
File files[] = new File(url.getPath()).listFiles();
for (int i = 0; i < files.length; i++) {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
ContentType strContent = ContentType.create("text/plain", Charset.forName("UTF-8"));
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("data", files[i]);
builder.addBinaryBody("template", xdpTemplate);
builder.addBinaryBody("options",GetOptions.getPDFOptions().getBytes(),ContentType.APPLICATION_JSON,"options"
try {
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = httpclient.execute(httpPost);
InputStream generatedPDF = response.getEntity().getContent();
byte[] bytes = IOUtils.toByteArray(generatedPDF);
File saveLocation = new File(SAVE_LOCATION);
if (!saveLocation.exists()) {
saveLocation.mkdirs();
}
File outputFile = new File(SAVE_LOCATION+File.separator+files[i].getName().replace("xml", "pdf"));
try (FileOutputStream outputStream = new FileOutputStream(outputFile)) {
outputStream.write(bytes);
}
} catch (Exception e) {
System.out.println("The error is " + e.getMessage());
}
}
System.out.println("Done generating " + files.length + " files");
}
}
Previous pageCreate Access Token
Next pageTest the solution
Experience Manager
- Overview
- Playlists
- Introduction to AEM as a Cloud Service
- Experience Cloud integrations
- Underlying Technology
- Edge Delivery Services
- Cloud Manager
- Local Development Environment Setup
- Developing
- Debugging AEM
- AEM APIs
- Content Delivery
- Caching
- Accessing AEM
- Authentication
- Advanced Networking
- Security
- AEM Eventing
- Migration
- Content Transfer Tool
- Bulk Import of assets
- Moving to AEM as a Cloud Service
- Cloud Acceleration Manager
- Content Fragments
- Forms
- Developing for Forms as a Cloud Service
- 1 - Getting started
- 2 - Install IntelliJ
- 3 - Setup Git
- 4 - Sync IntelliJ with AEM
- 5 - Build a form
- 6 - Custom Submit Handler
- 7 - Registering servlet using resource type
- 8 - Enable Forms Portal Components
- 9 - Include Cloud Services and FDM
- 10 - Context aware cloud configuration
- 11 - Push to Cloud Manager
- 12 - Deploy to development environment
- 13 - Updating maven archetype
- Create Adaptive Form
- Custom submit service with headless form
- Create address block component
- Create clickable image component
- AEM Forms and Analytics
- Creating Countries Dropdown Component
- Creating Button Variations
- Using vertical tabs
- Using output and forms service
- Document Generation in AEM Forms CS
- Using Forms Document Services API
- Document Generation using Batch API
- PDF Manipulation in Forms CS
- Integrate with Marketo
- Store Form Submissions with Blob Index Tags
- Prefill core component based form
- Azure Portal Storage
- Save and Resume form filling
- Create Review Workflow
- Acrobat Sign with AEM Forms
- Integrate with Microsoft Power Automate
- Integrate with Microsoft Dynamics
- Integrate with Salesforce
- Store form submissions in one drive and sharepoint
- Developing for Forms as a Cloud Service
- Asset Compute Extensibility
- Multi-step Tutorials
- Expert Resources