can the PDF created by ebase apps be password protected?

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

t4nu
Ebase User
Posts: 305
Joined: Thu Jul 02, 2015 8:32 am
Location: Indonesia

can the PDF created by ebase apps be password protected?

#1

Postby t4nu » Wed Apr 06, 2016 10:38 pm

Hi,
As the subject stated, I want to know if is it possible to password protect the PDF generated by ebase webapp? If so, how?
Thanks in advance for the help.
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#2

Postby Jon » Thu Apr 07, 2016 9:32 am

I google'd this and came up with this stack overflow example http://stackoverflow.com/questions/2568 ... -protected. In general, if you can find some Java code that does what you want, you can just convert it to Javascript:

The example uses this Java code:

Code: Select all

/** User password. */
public static byte[] USER = "Hello".getBytes();
/** Owner password. */
public static byte[] OWNER = "World".getBytes();

...

public void encryptPdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    stamper.setEncryption(USER, OWNER,
        PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
    stamper.close();
    reader.close();
}
Converted to Javascript it looks like this:

Code: Select all

importPackage(com.lowagie.text.pdf);
importPackage(java.io);

/** User password. */
var USER_PASSWORD = new java.lang.String("Hello").getBytes();
/** Owner password. */
var OWNER_PASSWORD = new java.lang.String("World").getBytes();

function encryptPdf(src, dest)  {
  var reader = new PdfReader(src);
  stamper = new PdfStamper(reader, new FileOutputStream(dest));
  stamper.setEncryption(USER_PASSWORD, OWNER_PASSWORD,
     PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
  stamper.close();
  reader.close();
}
I don't know anything about PDF protection so can't vouch for whether this works or not. But you should be able to create a solution from this. It seems to take an existing PDF file path as input (var src), and create a new PDF (at patht dest). This uses the itext PDF package which is already included in Ebase, but you will also need to import a bouncy castle provider jar (provides encryption) and add it to WEB-INF/lib on the server. I found this problematic and eventually got it to work with this jar file http://repo2.maven.org/maven2/org/bounc ... 5%2b/1.46/

Hope this points you in the right direction.
0 x

t4nu
Ebase User
Posts: 305
Joined: Thu Jul 02, 2015 8:32 am
Location: Indonesia

#3

Postby t4nu » Thu Apr 07, 2016 9:37 am

Hi,
Thanks a lot for the respond, Jon. Really appreciate a complete pointer.
I will try to elaborate it and find out if I can get what I want.

Jon wrote:I google'd this and came up with this stack overflow example http://stackoverflow.com/questions/2568 ... -protected. In general, if you can find some Java code that does what you want, you can just convert it to Javascript:

The example uses this Java code:

Code: Select all

/** User password. */
public static byte[] USER = "Hello".getBytes();
/** Owner password. */
public static byte[] OWNER = "World".getBytes();

...

public void encryptPdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    stamper.setEncryption(USER, OWNER,
        PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
    stamper.close();
    reader.close();
}
Converted to Javascript it looks like this:

Code: Select all

importPackage(com.lowagie.text.pdf);
importPackage(java.io);

/** User password. */
var USER_PASSWORD = new java.lang.String("Hello").getBytes();
/** Owner password. */
var OWNER_PASSWORD = new java.lang.String("World").getBytes();

function encryptPdf(src, dest)  {
  var reader = new PdfReader(src);
  stamper = new PdfStamper(reader, new FileOutputStream(dest));
  stamper.setEncryption(USER_PASSWORD, OWNER_PASSWORD,
     PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
  stamper.close();
  reader.close();
}
I don't know anything about PDF protection so can't vouch for whether this works or not. But you should be able to create a solution from this. It seems to take an existing PDF file path as input (var src), and create a new PDF (at patht dest). This uses the itext PDF package which is already included in Ebase, but you will also need to import a bouncy castle provider jar (provides encryption) and add it to WEB-INF/lib on the server. I found this problematic and eventually got it to work with this jar file http://repo2.maven.org/maven2/org/bounc ... 5%2b/1.46/

Hope this points you in the right direction.
0 x


Who is online

Users browsing this forum: No registered users and 23 guests