Question
Belvnesheconombank
BY
Last activity: 10 Oct 2024 8:52 EDT
How to convert JPEG to PDF
Hi guys!
I have the requirement to convert JPG to PDF. I already have pyFileSource property (I think that it is like a stream). I need to convert this stream to PDF format and place it instead of old pyFileSource property. How can I do it? Thanks in advance!
P.S. I already tried different ways from PDN articles
-
Like (0)
-
Share this page Facebook Twitter LinkedIn Email Copying... Copied!
Accepted Solution
Updated: 10 Oct 2024 8:52 EDT
Belvnesheconombank
BY
@KALINGAROUT thank you for help!
I found other solution. Code of my function you can see below:
@KALINGAROUT thank you for help!
I found other solution. Code of my function you can see below:
//Get Engine API library
PublicAPI api = (PublicAPI) ThreadContainer.get().getPublicAPI();
//find primary page
ClipboardPage mainPage = api.findPage("pyWorkPage");
//get value of property (it's a stream of uploaded file)
String IMGstr = mainPage.getString(".FoundingDocument.pyFileSource");
String IMGname = mainPage.getString(".FoundingDocument.pyFileName");
//convert stream to byte array
byte[] ImgArray = IMGstr.getBytes();
//Prepare byte array output stream
java.io.ByteArrayOutputStream outfile = new java.io.ByteArrayOutputStream();
//Create new document
com.lowagie.text.Document document = new com.lowagie.text.Document();
try{
// Get Instance of PDF writer
com.lowagie.text.pdf.PdfWriter writer = com.lowagie.text.pdf.PdfWriter.getInstance(document,outfile);
writer.setStrictImageSequence(true);
// Open the document
document.open();
//Get array
com.lowagie.text.Image jpg = com.lowagie.text.Image.getInstance(Base64Util.decodeToByteArray(IMGstr));
//Scale the image
float scaler = ((document.getPageSize().getWidth() - document.leftMargin() - document.rightMargin()) / jpg.getWidth()) * 100;
jpg.scalePercent(scaler);
//Add image to document and finish work with document
document.add(jpg);
document.close();
//set correct values to properties
mainPage.putString(".FoundingDocument.pyFileName", IMGname + "_converted.pdf");
mainPage.putString(".FoundingDocument.pyFileType", "pdf");
mainPage.putString(".FoundingDocument.pyNote", IMGname + "_converted");
//Encode result pdf and replace it instead of uploaded JPEG
String pdfStream = Base64Util.encodeToString(outfile.toByteArray());
mainPage.getProperty(".FoundingDocument.pyFileSource").setValue(pdfStream);
outfile.flush();
}catch(Exception e){
e.printstacktrace();
}
Maantic
IN
@Artsiom_Sukhanau - You can try using iText to convert jpg to pdf. Pega already ships with iText jar so I don't think we need to import anything.
https://thinktibits.blogspot.com/2011/06/convert-jpg-to-pdf-itext-java-example.html
Belvnesheconombank
BY
@KALINGAROUT hello! Thanks for your reply☺
But in that article we work with physical files
But I have a case and its clipboard. So I need to create logical PDF in Java with my pyFileSource property, convert it to stream back and replace current pyFileSource (which has stream of uploaded JPEG) with a stream of newly created PDF. So user after pressing the button "Download file" must get PDF instead of JPG.
Here is example of pyFileSource property (JPEG):
@KALINGAROUT hello! Thanks for your reply☺
But in that article we work with physical files
But I have a case and its clipboard. So I need to create logical PDF in Java with my pyFileSource property, convert it to stream back and replace current pyFileSource (which has stream of uploaded JPEG) with a stream of newly created PDF. So user after pressing the button "Download file" must get PDF instead of JPG.
Here is example of pyFileSource property (JPEG):
/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCABzAKUDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8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CLgnP+jtAxPJavHbn/g2m/aM8SWzad4i/wCCkfx71rQboeVfae8Op7buE/ejO/WXTkf3kYexr9g6KAPjH/gnV/wQU/Zy/wCCa722q+E/CZ8T+OoTvPi7xOU1DVEf1g+VYrbvzCiOQcMzV9nUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAf/9k=
Maantic
IN
@Artsiom_Sukhanau - Please refer the below link. In this they are converting the existing attachments of case into pdf file and then attaching it to the case. You can find the java code in the below link.
https://collaborate.pega.com/question/conversion-word-excel-documents-pdf-case-attachments
Accepted Solution
Updated: 10 Oct 2024 8:52 EDT
Belvnesheconombank
BY
@KALINGAROUT thank you for help!
I found other solution. Code of my function you can see below:
@KALINGAROUT thank you for help!
I found other solution. Code of my function you can see below:
//Get Engine API library
PublicAPI api = (PublicAPI) ThreadContainer.get().getPublicAPI();
//find primary page
ClipboardPage mainPage = api.findPage("pyWorkPage");
//get value of property (it's a stream of uploaded file)
String IMGstr = mainPage.getString(".FoundingDocument.pyFileSource");
String IMGname = mainPage.getString(".FoundingDocument.pyFileName");
//convert stream to byte array
byte[] ImgArray = IMGstr.getBytes();
//Prepare byte array output stream
java.io.ByteArrayOutputStream outfile = new java.io.ByteArrayOutputStream();
//Create new document
com.lowagie.text.Document document = new com.lowagie.text.Document();
try{
// Get Instance of PDF writer
com.lowagie.text.pdf.PdfWriter writer = com.lowagie.text.pdf.PdfWriter.getInstance(document,outfile);
writer.setStrictImageSequence(true);
// Open the document
document.open();
//Get array
com.lowagie.text.Image jpg = com.lowagie.text.Image.getInstance(Base64Util.decodeToByteArray(IMGstr));
//Scale the image
float scaler = ((document.getPageSize().getWidth() - document.leftMargin() - document.rightMargin()) / jpg.getWidth()) * 100;
jpg.scalePercent(scaler);
//Add image to document and finish work with document
document.add(jpg);
document.close();
//set correct values to properties
mainPage.putString(".FoundingDocument.pyFileName", IMGname + "_converted.pdf");
mainPage.putString(".FoundingDocument.pyFileType", "pdf");
mainPage.putString(".FoundingDocument.pyNote", IMGname + "_converted");
//Encode result pdf and replace it instead of uploaded JPEG
String pdfStream = Base64Util.encodeToString(outfile.toByteArray());
mainPage.getProperty(".FoundingDocument.pyFileSource").setValue(pdfStream);
outfile.flush();
}catch(Exception e){
e.printstacktrace();
}