Question
Accenture
CN
Last activity: 9 Aug 2017 3:31 EDT
How Can I put the QR Code on a print report(Exp:Excel or Pdf)?
Hi All,
I have a issue on Develope a pega application.Now I have created a QR Code with pega. And I want put the QR Code on a Excel file(Use OOTB:MSOGenerateExcelFile) or Pdf File.That customer can scan it with smart phone.
How should I do it? Please help me~
Thank you.
***Updated by Moderator: Lochan. Removed user added Ask the Expert tag. Apologies for confusion, shouldn't have been an end-user option***
-
Like (0)
-
Share this page Facebook Twitter LinkedIn Email Copying... Copied!
Accepted Solution
Pegasystems Inc.
GB
Here's a working prototype of an Activity which generated a QR code based on the text you provide: and then stores the image as a PRPC Data-Content-Image instance.
You can then reference this image in a HTML rule: which can then be converted to a PDF (I have attached an example).
Here's a working prototype of an Activity which generated a QR code based on the text you provide: and then stores the image as a PRPC Data-Content-Image instance.
You can then reference this image in a HTML rule: which can then be converted to a PDF (I have attached an example).
Here's the expanded steps (for those steps that have extra info only):
Here's the Java code for Step 2:
com.google.zxing.qrcode.QRCodeWriter writer = new com.google.zxing.qrcode.QRCodeWriter();
com.google.zxing.common.BitMatrix matrix = null;try {
matrix = writer.encode(text, com.google.zxing.BarcodeFormat.QR_CODE, width, height);
}
catch (com.google.zxing.WriterException e) { throw new PRRuntimeException(e); }java.awt.image.BufferedImage image = new java.awt.image.BufferedImage(width, height, java.awt.image.BufferedImage.TYPE_INT_RGB);
image.createGraphics();java.awt.Graphics2D graphics = (java.awt.Graphics2D) image.getGraphics();
graphics.setColor(java.awt.Color.YELLOW);
graphics.fillRect(0, 0, width, height);
graphics.setColor(java.awt.Color.BLUE);for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (matrix.get(x, y) == true) {
graphics.fillRect(x, y, 1, 1);
}
}
}try {
java.io.ByteArrayOutputStream bos=new java.io.ByteArrayOutputStream();
javax.imageio.ImageIO.write(image, filetype, bos );
bos.close();
imagebytes=bos.toByteArray();
com.pega.pegarules.pub.util.Base64Util encoder=new com.pega.pegarules.pub.util.Base64Util();
b64=encoder.encodeToString( (byte[])imagebytes );}
catch(Exception e) { throw new PRRuntimeException(e); }
And for Step 3:
try {
java.security.MessageDigest hasher=java.security.MessageDigest.getInstance("SHA1");
hasher.update( (byte[]) imagebytes);
byte[] hash=hasher.digest();
StringBuilder sb = new StringBuilder(2 * hash.length);
for (byte b : hash) {
sb.append("0123456789ABCDEF".charAt((b & 0xF0) >> 4));
sb.append("0123456789ABCDEF".charAt((b & 0x0F)));
}
filename = sb.toString();
}
catch(Exception e) { throw new PRRuntimeException(e); }
Here's an example of running the Activity:
Now we need to check the PRPC logs (or we can check the returned parameter) to get the allocated image id:
Here's the text of that logfile entry:
2016-08-12 17:34:45,480 [http-bio-7210-exec-5] [ STANDARD] [ ] [ GCSApp:01.01.01] (eQRCode.GCS_GCSApp_Work.Action) INFO prpchost| Proprietary information hidden Admin@GCS - Created Image/qrcode/BEB63875D4E6215918AD33DAB4B8CF6F9B5C4D3A.png for text:Hello From the PSC!
Now we can look for the new image under the 'Data-Content-Image' class: I added the class to my 'pinned classes' and then double-clicked the classname, to list all the instances on the right hand side:
Here's the image: you can scan it to check :-)
And we can double-check the history tab:
Here's an example of how to embed the QR code in an HTML rule:
Here's the source of the HTML above:
<html>
<head>
<title>QR Test</title>
<style type="text/css">
div.boxed { border-style: solid; }
</style>
</head>
<body>
QR CODE TEST: <pega:contentURL name="BEB63875D4E6215918AD33DAB4B8CF6F9B5C4D3A.png" path="/qrcode/" classType="Image" />
<br/>
<div class='boxed'>
<img src='<pega:contentURL name="BEB63875D4E6215918AD33DAB4B8CF6F9B5C4D3A.png" path="/qrcode/" classType="Image"/>' >
</div></body>
</html>
We can preview the HTML:
And here's the image (and the URL included as text):
==============
Pegasystems Inc.
GB
Is the QR code saved in a binary image format such as JPG/GIF/PNG ?
If so: try including a 'img' reference to that image in an HTML file. and then use HTMLTOPDF to render the HTML to a PDF.
In order to output to EXCEL: it's more complicated - but still maybe possible.
I have never done this: but possible the Apache POI library includes API Calls to embedd images in an EXCEL file ? PRPC ships with Apache POI. Again: I have never done this - but possibly the 'Microsoft Office XML Formats' allow (if so , they probably do it via a BASE64 mechanism) for embedding images also ?
If you QR code is NOT a binary file (so for instance, you have used a Javascript library to dynamically 'draw' the image to the browser window : which is currently how the Designer Studio generates QR codes for App Shortcuts etc) : then the above mechanisms will not work.
Thanks,
John
Accenture
CN
Hi John,
Thanks for your quickly repeat.
I was create QR with a Javascript like PzURLQR. So it is automatic with CaseID.About this condition could you give some suggestion to me?
Thanks,
Pegasystems Inc.
GB
Hi Chungliang,
If you are using a Javascript-based mechanism to create a QR code; that isn't going to work (easily anyway) with a Server-Based solution.
You should look at a Java-Based method for generating a QR Code as a Bitmap (and thence a PNG/JPG/GIF etc); a good one that I have used is the 'zxing' ('Zebra Crossing') library; which is available here : https://github.com/zxing/zxing
I don't have an example of working with this library in PRPC that I can share at this time I'm afraid - but there are some good standalone examples out there on the Web.
You will need to also go through the process of importing these Third-Party JARs into PRPC to work with them directly.
As an alternative to importing the JARs: you might want to consider creating a simple Webapp (just a simple WAR file with a single Servlet probably) that will accept (probably) a POST request and return a PNG (etc) of the QR code in response; this second approach has two advantages over the first:
1. There are lots of examples of doing this sort of thing already on the web.
2. You should then be able to use a PRPC HTTP Connector to interface to this secondary web app; so it will be relatively simple , de-coupled way of achieving this.
Pegasystems Inc.
GB
Unfortunately: it looks like an externally refernced image file in a HTML file will NOT be included in a HTMLTOPDF (or at least that's what my tests so far show).
So : you would have to make the sure QR code image is available to PRPC as Static Content, or possibly a in-memory (probably rendered as base64 'inline' image data in a HTML file) byte-array of the JPG (etc) image data.
Despite my test not working; I ended up creating a simple (and very badly written - needs quite a bit doing to it, to make it 'safe') Servlet using ZXING library to generate a dynamic QR code.
This Servlet works: and creates a dynamic Image File based on the Servlet Parameters; but as I say , including a reference to the IMG in a PRPC HTML Rule and then converting that Rule does NOT show the image in the resultant PDF (I found at least).
But the code snippets maybe useful to you anyway: perhaps you want to try converting the code into a PRPC Function/Activity here ? (there is only one required third-party JAR used, here's it's Maven Coordinates:
com.google.zxing | core | 2.2
(I used an older version here, there are much newer versions available as well).
Here's the method used to generate the QR Code given some input text, and an image width/height, mostly stolen , erm I mean , borrowed code from around the web:
Unfortunately: it looks like an externally refernced image file in a HTML file will NOT be included in a HTMLTOPDF (or at least that's what my tests so far show).
So : you would have to make the sure QR code image is available to PRPC as Static Content, or possibly a in-memory (probably rendered as base64 'inline' image data in a HTML file) byte-array of the JPG (etc) image data.
Despite my test not working; I ended up creating a simple (and very badly written - needs quite a bit doing to it, to make it 'safe') Servlet using ZXING library to generate a dynamic QR code.
This Servlet works: and creates a dynamic Image File based on the Servlet Parameters; but as I say , including a reference to the IMG in a PRPC HTML Rule and then converting that Rule does NOT show the image in the resultant PDF (I found at least).
But the code snippets maybe useful to you anyway: perhaps you want to try converting the code into a PRPC Function/Activity here ? (there is only one required third-party JAR used, here's it's Maven Coordinates:
com.google.zxing | core | 2.2
(I used an older version here, there are much newer versions available as well).
Here's the method used to generate the QR Code given some input text, and an image width/height, mostly stolen , erm I mean , borrowed code from around the web:
public static BufferedImage encodeToQrCode(final String text, final int width, final int height) {
QRCodeWriter writer = new QRCodeWriter();
BitMatrix matrix = null;
try {
matrix = writer.encode(text, BarcodeFormat.QR_CODE, width, height);
} catch (WriterException e) {
throw new RuntimeException(e);
}BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
image.createGraphics();Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(Color.YELLOW);
graphics.fillRect(0, 0, width, height);
graphics.setColor(Color.BLUE);for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (matrix.get(x, y) == true) {
graphics.fillRect(x, y, 1, 1);
}
}
}
return image;
}
And here's the method in the Servlet that delivers the Image to a HTTP client (but if you plan to implement the above in PRPC, you won't need most of this - I include it only because it deals a bit with converting the BufferedImage into bytes, which you probably will need to whatever method you end up using).
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TBD: guard against missing/bad parameters.
String text=request.getParameter("t");
int width=Integer.parseInt( request.getParameter("w") );
int height=Integer.parseInt( request.getParameter("h") );
String format=request.getParameter("f");
BufferedImage image=encodeToQrCode(text, width, height); // sloppy; what happens if the image creation fails ?
BufferedOutputStream sbos=null;
ByteArrayOutputStream bos=null;
try {
bos=new ByteArrayOutputStream();
sbos = new BufferedOutputStream(response.getOutputStream());
ImageIO.write(image, format, bos);
String MimeType=String.format("image/%s", format); // this is slapdash: should be 'image/jpeg', not 'image/jpg' for instance
response.setContentType(MimeType);
response.setContentLength( bos.size() );
sbos.write(bos.toByteArray());} finally {
bos.close();
sbos.close();}
}
I'll attach my Maven Project (WAR/Web Project) as well here, as well as a 'WAR' file (which you should be able to deploy and use to see it working, even though it probably won't help you here).
Actually; as I write this; possibly you could still use this approach (separate WAR to generate the QR): and still use PRPC with a HTTP Rule to fetch the data and then stuff it into a PRPC Property, then convert that into base64, and then use that base64 as an inline image ? dunno (I haven't tested what HTMLTOPDF does with inline base64 images).
-
Vijender Malik Suresh Kumar R Kalyan Polu Telaprolu Naveen
Pegasystems Inc.
GB
Hi Chungliang,
Ok: this does NOT work yet: but I'll share what I have so far.
In order to even have a chance of placing a QR code on a (server-generated) file such as a PDF or EXCEL file; you'll need to generate the QR code on the server-side (rather than in Javascript).
I previously said the Google's ZXing might be a decent way of doing this.
Here's some (as I say, incomplete) notes about how to go about doing this:
1. Get hold of the ZXing 'core' JAR (you can change to a newer version if you want):
It is available from the Maven Repos here (just use a browser to download it): https://repo1.maven.org/maven2/com/google/zxing/core/2.2/core-2.2.jar
2. Then you can import the JAR into PRPC using the following steps:
Designer Studio | Distribution | Import
Hi Chungliang,
Ok: this does NOT work yet: but I'll share what I have so far.
In order to even have a chance of placing a QR code on a (server-generated) file such as a PDF or EXCEL file; you'll need to generate the QR code on the server-side (rather than in Javascript).
I previously said the Google's ZXing might be a decent way of doing this.
Here's some (as I say, incomplete) notes about how to go about doing this:
1. Get hold of the ZXing 'core' JAR (you can change to a newer version if you want):
It is available from the Maven Repos here (just use a browser to download it): https://repo1.maven.org/maven2/com/google/zxing/core/2.2/core-2.2.jar
2. Then you can import the JAR into PRPC using the following steps:
Designer Studio | Distribution | Import
Then (as indicated in the screenshot above) - you will need to restart PRPC.
Once you have rebooted, you should then have access to the Google ZXing library; and you can create an Activity like this:
Here's the Java Code from the Java Step above:
com.google.zxing.qrcode.QRCodeWriter writer = new com.google.zxing.qrcode.QRCodeWriter();
com.google.zxing.common.BitMatrix matrix = null;try {
matrix = writer.encode(text, com.google.zxing.BarcodeFormat.QR_CODE, width, height);
}
catch (com.google.zxing.WriterException e) { throw new PRRuntimeException(e); }java.awt.image.BufferedImage image = new java.awt.image.BufferedImage(width, height, java.awt.image.BufferedImage.TYPE_INT_RGB);
image.createGraphics();java.awt.Graphics2D graphics = (java.awt.Graphics2D) image.getGraphics();
graphics.setColor(java.awt.Color.YELLOW);
graphics.fillRect(0, 0, width, height);
graphics.setColor(java.awt.Color.BLUE);for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (matrix.get(x, y) == true) {
graphics.fillRect(x, y, 1, 1);
}
}
}
Now , this Activity runs without error, but I haven't yet done anything with the resultant BufferedImage, so nothing of interest happens when you run it !
The next stage is to convert that BufferedImage into a JPG/GIF/PNG image format ; but even after doing that - I'm not sure on the best way of saving this Image into PRPC (so that it can be subseqeuently embedded into a HTML, which could be converted to a PDF (say) ).....
Maybe somebody else has some ideas on doing that ?
Pegasystems Inc.
GB
Slight improvement: now writing out the QR code (it's currently fixed as 'png') to a file on the PRPC 'ServiceExport' directory; error-checking and some logic is dubious at the moment; but works.
Java step modified as follows:
com.google.zxing.qrcode.QRCodeWriter writer = new com.google.zxing.qrcode.QRCodeWriter();
com.google.zxing.common.BitMatrix matrix = null;
Slight improvement: now writing out the QR code (it's currently fixed as 'png') to a file on the PRPC 'ServiceExport' directory; error-checking and some logic is dubious at the moment; but works.
Java step modified as follows:
com.google.zxing.qrcode.QRCodeWriter writer = new com.google.zxing.qrcode.QRCodeWriter();
com.google.zxing.common.BitMatrix matrix = null;try {
matrix = writer.encode(text, com.google.zxing.BarcodeFormat.QR_CODE, width, height);
}
catch (com.google.zxing.WriterException e) { throw new PRRuntimeException(e); }java.awt.image.BufferedImage image = new java.awt.image.BufferedImage(width, height, java.awt.image.BufferedImage.TYPE_INT_RGB);
image.createGraphics();java.awt.Graphics2D graphics = (java.awt.Graphics2D) image.getGraphics();
graphics.setColor(java.awt.Color.YELLOW);
graphics.fillRect(0, 0, width, height);
graphics.setColor(java.awt.Color.BLUE);for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (matrix.get(x, y) == true) {
graphics.fillRect(x, y, 1, 1);
}
}
}try {
PRFile prFile=new PRFile( filename );
PROutputStream pros=new PROutputStream( prFile );
javax.imageio.ImageIO.write(image, "png", pros );
pros.close();}
catch(Exception e) { throw new PRRuntimeException(e); }
The file will be created on the PRPC backend as:
[...]/PegaTemp/StaticContent/global/ServiceExport/<text of qr code>.png
And this locationis available as a URL (but I haven't encoded that into the Activity yet)
Accepted Solution
Pegasystems Inc.
GB
Here's a working prototype of an Activity which generated a QR code based on the text you provide: and then stores the image as a PRPC Data-Content-Image instance.
You can then reference this image in a HTML rule: which can then be converted to a PDF (I have attached an example).
Here's a working prototype of an Activity which generated a QR code based on the text you provide: and then stores the image as a PRPC Data-Content-Image instance.
You can then reference this image in a HTML rule: which can then be converted to a PDF (I have attached an example).
Here's the expanded steps (for those steps that have extra info only):
Here's the Java code for Step 2:
com.google.zxing.qrcode.QRCodeWriter writer = new com.google.zxing.qrcode.QRCodeWriter();
com.google.zxing.common.BitMatrix matrix = null;try {
matrix = writer.encode(text, com.google.zxing.BarcodeFormat.QR_CODE, width, height);
}
catch (com.google.zxing.WriterException e) { throw new PRRuntimeException(e); }java.awt.image.BufferedImage image = new java.awt.image.BufferedImage(width, height, java.awt.image.BufferedImage.TYPE_INT_RGB);
image.createGraphics();java.awt.Graphics2D graphics = (java.awt.Graphics2D) image.getGraphics();
graphics.setColor(java.awt.Color.YELLOW);
graphics.fillRect(0, 0, width, height);
graphics.setColor(java.awt.Color.BLUE);for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (matrix.get(x, y) == true) {
graphics.fillRect(x, y, 1, 1);
}
}
}try {
java.io.ByteArrayOutputStream bos=new java.io.ByteArrayOutputStream();
javax.imageio.ImageIO.write(image, filetype, bos );
bos.close();
imagebytes=bos.toByteArray();
com.pega.pegarules.pub.util.Base64Util encoder=new com.pega.pegarules.pub.util.Base64Util();
b64=encoder.encodeToString( (byte[])imagebytes );}
catch(Exception e) { throw new PRRuntimeException(e); }
And for Step 3:
try {
java.security.MessageDigest hasher=java.security.MessageDigest.getInstance("SHA1");
hasher.update( (byte[]) imagebytes);
byte[] hash=hasher.digest();
StringBuilder sb = new StringBuilder(2 * hash.length);
for (byte b : hash) {
sb.append("0123456789ABCDEF".charAt((b & 0xF0) >> 4));
sb.append("0123456789ABCDEF".charAt((b & 0x0F)));
}
filename = sb.toString();
}
catch(Exception e) { throw new PRRuntimeException(e); }
Here's an example of running the Activity:
Now we need to check the PRPC logs (or we can check the returned parameter) to get the allocated image id:
Here's the text of that logfile entry:
2016-08-12 17:34:45,480 [http-bio-7210-exec-5] [ STANDARD] [ ] [ GCSApp:01.01.01] (eQRCode.GCS_GCSApp_Work.Action) INFO prpchost| Proprietary information hidden Admin@GCS - Created Image/qrcode/BEB63875D4E6215918AD33DAB4B8CF6F9B5C4D3A.png for text:Hello From the PSC!
Now we can look for the new image under the 'Data-Content-Image' class: I added the class to my 'pinned classes' and then double-clicked the classname, to list all the instances on the right hand side:
Here's the image: you can scan it to check :-)
And we can double-check the history tab:
Here's an example of how to embed the QR code in an HTML rule:
Here's the source of the HTML above:
<html>
<head>
<title>QR Test</title>
<style type="text/css">
div.boxed { border-style: solid; }
</style>
</head>
<body>
QR CODE TEST: <pega:contentURL name="BEB63875D4E6215918AD33DAB4B8CF6F9B5C4D3A.png" path="/qrcode/" classType="Image" />
<br/>
<div class='boxed'>
<img src='<pega:contentURL name="BEB63875D4E6215918AD33DAB4B8CF6F9B5C4D3A.png" path="/qrcode/" classType="Image"/>' >
</div></body>
</html>
We can preview the HTML:
And here's the image (and the URL included as text):
==============
-
Vaibhav Ranadive
Accenture
CN
Hi John,
Sorry about reply late.I went on a trip.
Your reply is Great. I will try to do this. And tell you the result.Thank you very much.
best regards,
Chunliang
macquarie group ltd
AU
Hi,
Is there anyway to read/scan the QR code within Pega from a pdf .