简体   繁体   中英

How to write an incoming JSON payload to a pdf file using Mule 4

I'm trying to convert JSON to a pdf file but when I am trying to open the file getting the "Failed to load PDF document" error in mule 4 (runtime 4.2.1). How can I fix this issue?

code:

 <?xml version="1.0" encoding="UTF-8"?>
  <mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core 
  http://www.mulesoft.org/schema/mule/core/current/mule.xsd
 http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
    http://www.mulesoft.org/schema/mule/ee/core 
     http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
   http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd">

  <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="79ef9cde-39c4-449a-8cac-9aeb3be30c27" >
    <http:listener-connection host="0.0.0.0" port="8084" />
</http:listener-config>
<file:config name="File_Config" doc:name="File Config" doc:id="d67cbdb7-3bc8-4f04-8d7f-862c539f5f3f" >
    <file:connection workingDir="D:\Devendra\workspace_dev\mule4\pdf\src\main\resources\pdf" />
</file:config>
<flow name="pdfFlow" doc:id="eaef4b93-c6f1-4e01-9df3-93e2ba2dcba4" >
    <http:listener doc:name="Listener" doc:id="1407afe8-dbea-4f67-8514-29b8039c5638" config-ref="HTTP_Listener_config" path="/pdf"/>
    <set-payload value="#[payload]" doc:name="Set Payload" doc:id="a250f9a4-c633-4ce1-a75d-8668d2dede10" mimeType="application/json"/>
    <file:write doc:name="Write" doc:id="ca2c47a2-924d-445a-8ca1-7de6197cd583" path="test.pdf" config-ref="File_Config"/>
    <logger level="INFO" doc:name="Logger" doc:id="0c629051-500e-4bc0-82b4-74b6c79ec567" message="#[payload]"/>
</flow>
  </mule>

I suspect that the problem is that the flow is writing a JSON file to a file called test.pdf. If you try to open with a PDF reader, it will fail because a JSON document is not a PDF file.

<set-payload value="#[payload]" doc:name="Set Payload" doc:id="a250f9a4-c633-4ce1-a75d-8668d2dede10" mimeType="application/json"/>

Mule doesn't have the feature to convert to PDF, so you will need to implement that transformation using Java code calling some PDF generation library.

It is not possible in dataweave st away, so please write a java code. Below is the Java code that will help you

Put that java class inside src/main/java/com/pdf/JsonToPDF.java folder

package com.pdf;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import com.itextpdf.text.Document;

import com.itextpdf.text.DocumentException;

import com.itextpdf.text.Paragraph;

import com.itextpdf.text.pdf.PdfWriter;


public class JsonToPDF {

public static void jsontopdf(String filename,String content) {

Document document = new Document();

try

{

File file = new File(filename);

PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));

document.open();

document.add(new Paragraph(content));

document.close();

writer.close();

} catch (DocumentException e)

{

e.printStackTrace();

} catch (FileNotFoundException e)

{

e.printStackTrace();

}


}


}

and call this static method in your Dataweave. Below is the dataweave code


%dw 2.0

import java!com::pdf::JsonToPDF

output application/java

---

{

pdf :JsonToPDF::jsontopdf("C:\\Users\\nanmanikanta\\Desktop\\Integration\\test.pdf","{

'test': 'test'


}")

}

You can change this java to just write a pdf or make this class to return a pdf filestream and give to other processors.

You can multiple things.

For your reference here is the itext library sample code links :

https://howtodoinjava.com/library/read-generate-pdf-java-itext/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM