Question
Can we display inline bytes from pdf/images in Section
Hi all,
1. How do I display attachment on the Section e.g. via Content-Disposition inline.
The code below is a simple test to read PDF bytes (the bytes could be read from URL etc). How can we do this in Pega?
package sc.test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class MyServlet2_1
*/
@WebServlet("/MyServlet2_1")
public class MyServlet2_1 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MyServlet2_1() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req,res);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
ServletOutputStream out = res.getOutputStream();
res.setContentType("application/pdf"); // MIME type for pdf doc
res.setHeader("Content-disposition", "inline; filename="
+ "Example.pdf");
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(out);
byte[] buff = getByte();
int bytesRead;
bos.write(buff, 0, buff.length);
} catch (final MalformedURLException e) {
System.out.println("MalformedURLException.");
throw e;
} catch (final IOException e) {
System.out.println("IOException.");
throw e;
} finally {
if (bos != null)
bos.close();
}
}
public byte[] getByte() {
FileInputStream fileInputStream = null;
File file = new File("C:\\temp2\\a.pdf");
byte[] bFile = new byte[(int) file.length()];
try {
// convert file into array of bytes
fileInputStream = new FileInputStream(file);
fileInputStream.read(bFile);
fileInputStream.close();
System.out.println("Done");
} catch (Exception e) {
e.printStackTrace();
}
return bFile;
}
2. How do I use PM4ML to create a PDF and so I can get the bytes used for the above question.
3. Is ITEXT PDF generation supported, can we use itext in Java activity or rules?
Regards
Seri
I don't know the answer to your first question, but I can help with questions #2 and #3.
You can convert HTML content to a byte array containing PDF content by calling the HTMLToPDF activity in @baseclass. Refer to the Description and Usage fields on the History tab of the activity to understand how it works.
iText capabilities are encapsulated in the GenerateEForm and ConcatenateEForms activities. These are not that well documented, and can be a bit more difficult to use.
In both cases, there are Java API calls that wrap direct access to either the PD4ML or iText libraries. There is nothing to prevent you from calling these libraries directly from java, but we do not recommend this.