Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, i'am new to Perl programming. I'was tring the functionalities to perform pdf file encryption and decryption. working on linux environment, installed PDF::Tk and corresponding binary file. Please find below the sorce code for tring to encryption (apps.pdf file).

sorce code:

use PDF::Tk; my $doc=PDF::Tk->new(pdftk=>'/apps/free/pdftk/1.44/bin/pdftk'); $doc->call_pdftk('apps.pdf', 'test.pdf','owner_pw','abc12345');

getting error as below:

Error: Unexpected command-line data: owner_pw where we were expecting an input PDF filename, operation (e.g. "cat") or "input_pw". Exiting. Errors encountered. No output created. Done. Input errors, so no output created. pdftk apps.pdf owner_pw abc12345 test.pdf failed: 256 at /usr/lib/perl5/site_perl/5.10.0/PDF/Tk.pm line 73.

note: created a new pdf 'apps.pdf' with Document Open Password as 'abc12345' and permission Password as 'abcd'.

Please help me in resolving the above issue.

Replies are listed 'Best First'.
Re: pdftk encryption function
by roboticus (Chancellor) on Sep 18, 2014 at 11:07 UTC

    The PDF::Tk documentation clearly states that the call_pdftk method "Calls up pdftk command, takes input, output and pdftk operation as arguments input and output can either be files or scalar refs. input can also be an array ref of files". I don't see any indication that it will take anything other than three arguments.

    When I look at the PDFTk site, I can see that they have a product called PDFtk Server that gives you command-line capabilities. On their examples page, it even shows example command lines for encrypting and decrypting PDFs. So perhaps you might try PDFtk Server and use perl's system function (perldoc -f system) to call it under program control.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      pdftk server (pdftk binary file) for linux has been already installed and passing the executable binary file value in 2nd line of source code.

      Please let me how to resolve it.

        sumalatha:

        Please read my post again--I told you how you may resolve it.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: pdftk encryption function
by soonix (Chancellor) on Sep 19, 2014 at 10:19 UTC
    owner_pw where we were expecting an input PDF filename, operation (e.g. "cat") or "input_pw"

    Just checked the page of my local pdftk: Seems like above message wants to tell you that "owner_pw" is neither a pdf-filename nor an operation nor "input_pw". ("input_pw" obviously is special)

    Try adding a command, e.g. simply "cat"

    $doc->call_pdftk('apps.pdf', 'test.pdf', 'cat', 'owner_pw','abc12345');

      passed a cat as argument before owner_pw, getting error as below

      Error: expecting page ranges. Instead, I got: owner_pw Errors encountered. No output created. Done. Input errors, so no output created. pdftk apps.pdf cat owner_pw 12345 test.pdf failed: 256 at /usr/lib/perl5/site_perl/5.10.0/PDF/Tk.pm line 73.

        please let me know are there are any other modules/ ways to perform pdf file encryption and decryption functions

        please help me , its of urgent for my final year project

Re: pdftk encryption function
by soonix (Chancellor) on Sep 23, 2014 at 09:43 UTC
    Didn't have PDF::Tk installed, therefore my first answer was wrong. Looking at the source of PDF::Tk, it seens to me it would have to be modified to fit your needs, better use
    system ('/apps/free/pdftk/1.44/bin/pdftk', 'apps.pdf', 'output', 'test +.pdf','owner_pw','abc12345') or die ...;
    (or
    system '/apps/free/pdftk/1.44/bin/pdftk apps.pdf output test.pdf owner +_pw abc12345' or die ...
    or use qx). Note the additional output in the command, PDF::Tk does insert this, too, but not where you would want it.

      Thanks for the solution, its working.

      Result of encryption is created a output pdf with password secured rite

      Note: "owner_pw" contains the permission password and "user_pw" contain open document password.

      And Decryption function it just remove the security password by removing owner_pw, but not user_pw

      Please let me know , whether my understanding is correct.

        • there is no encryption at work with regards to the owner password. The password itself probably is encrypted, but otherwise it is only used to indicate to the reading software, which permissions are to be granted. There are PDF readers and tools that ignore these specifications (pdftk not being one of them).
        • The user password however, is in fact used to encrypt the file.
        For more on this consult Wikipedia