GSai
GSai

Reputation: 131

How to programatically open a PDF with a User password

This is related to my other question... hope this one has a solution.

The requirement is to display a password-protected PDF in the browser but to pass the User password programatically. I create a PDF using Jasper and set the user password as follows:
exporter.setParameter(JRPdfExporterParameter.USER_PASSWORD, userPassword);

As soon as the PDF is created, it has to be displayed in the screen. While displaying in the browser, the user should not be prompted to key in the password ans hence the password should be supplied by the application However, if the user downloads the PDF and then tries to open it, he should be prompted to enter the password.
[Edit]: I am looking for an approach that does NOT involve licensed tools

Upvotes: 0

Views: 13174

Answers (4)

learn here
learn here

Reputation: 21

You can use pdf.js of mozilla to render password protected PDF. The below url that will prompt for password, until the correct password is given. The password for the pdf is "test".

http://learnnewhere.unaux.com/pdfViewer/passwordviewer.html

Here is the sample code for prompting password

pdfJs.onPassword = function (updatePassword, reason) { 
      if (reason === 1) { // need a password
            var new_password= prompt('Please enter a password:');
            updatePassword(new_password);
        } else { // Invalid password
            var new_password= prompt('Invalid! Please enter a password:');
            updatePassword(new_password);
        }
   };

If you want to close the password prompt on unsuccessful password attempts you can remove the else part(// Invalid password).

You could get the complete code from here https://github.com/learnnewhere/simpleChatApp/tree/master/pdfViewer

Upvotes: 1

Useful Angle
Useful Angle

Reputation: 986

You can open a password protected PDF using the PDF.JS library.

PDFJS.getDocument({ url: pdf_url, password: pdf_password }).then(function(pdf_doc) {
    // success
}).catch(function(error) {
    // incorrect password

    // error is an object having 3 properties : name, message & code
});

I've written a blog post on it, also containing a demo. This is the link : http://usefulangle.com/post/22/pdfjs-tutorial-2-viewing-a-password-protected-pdf

Upvotes: 4

basav
basav

Reputation: 1453

I'm not sure whether something of this is possible. On the browser the pdf is opened by a Plugin - usually Adobe Reader plug-in. There are also other makes apart from Adobe Reader. Chrome has it own plugin.

On the browser when it detects any PDF file - the rendering plugin takes over - and this is browser specific. You hardly have any control.

Easy alternative is to show the same content in a web page - probably a modal window if the content is sensitive and give a link to download the password protected pdf file

my 2c

Upvotes: 0

oddstar
oddstar

Reputation: 554

You could checkout PDF.js, an open source client based PDF renderer that also has support for encrypted PDFs. http://mozilla.github.com/pdf.js/

This means you will have to put your password somewhere in the javascript though, so you will have to disguise it, but it should do the trick :)

Upvotes: 1

Related Questions