0

I'm trying to give my users the option of password protecting the PDFs they create via PDFLib. I've read the documentation regarding PLOP, and came across the following paragraph in appendix A of the the manual (located here):

Memory-Based Combination The memory-based method is faster, but requires more memory. It is recommended for dynamic PDF generation and signature in Web applications unless you deal with very large documents. Instead of generating a PDF file on disk with PDFlib, use in-core PDF generation by supplying an empty file name to PDF_begin_document( ), fetch the contents of the buffer containing the generated PDF data using PDF_get_buffer( ), and create a virtual file with PLOP_create_pvf( ) . The file name used for the virtual file can then be passed to PLOP/PLOP DS using PLOP_open_document( ) without having to create a physical file on disk. Note that it is not possible to fetch the PDFlib buffer contents in multiple portions since the full document must be supplied to PLOP/PLOP DS in a single buffer. Therefore you must call PDF_get_buffer( ) between PDF_end_document( ) and PDF_delete( ). The hellosign programming sample, which is included in all PLOP packages, demonstrates how to use PDFlib for dynamically creating a PDF document and passing it to PLOP in memory for applying a digital signature.

So far I have the following method written, to be called prior to PDF_end_document(), as the manual instructs:

function encrypt_pdf($pdf_buffer, $password) {
    $optlist = '';
    $filename = "temp";
    create_pvf($filename, $pdf_buffer, $optlist);   

    $optlist = "masterpassword=$password";

    open_document($filename, $optlist);
    $doc = create_file($filename, $optlist);

}

I have no idea how to proceed from here. There is no documentation that I've found that even remotely covers what I'm trying to do (even though I imagine this is a common usage of the PLOP API).

How can I complete this method, and password protect my output PDF?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Matt
  • 6,993
  • 4
  • 29
  • 50
  • Are you trying to password protect the file on disk, or just ask the user for a password when they try and view it in your web app? – Justin Wood Oct 03 '12 at 20:52
  • @JustinWood The way this is supposed to work, is that it creates a PDF file that will not open unless a password is provided. I already have functionality that creates the file and sends it to the user; I just need to implement password protection. – Matt Oct 03 '12 at 20:55
  • @JustinWood PDFLib provides this functionality through their PLOP API, but the documentation is not helpful. – Matt Oct 03 '12 at 20:56

2 Answers2

2

Matt,

when you use PDFlib to create PDF documents (it sounds that you already to this), it's not necessary to use an additional library for secure the file. Simple use the permissions- options within the begin_document() option list.

You find a sample in the PDFlib cookbook http://www.pdflib.com/en/pdflib-cookbook/general-programming/permission-settings/php-permission-settings/ where you can see how to do this.

A detailed introduction in this topic, is located in the PDFlib 8 Tutorial, chapter 3.3 and PDFlib 8 API reference, chapter 3.1, table 3.1 which is included in all PDFlib 8 packages and also available for free download on http://www.pdflib.com/developer/technical-documentation/manuals/ (please do not use the php.net documentation page for PDFlib API)

When you do not work with PDFlib for creating the PDF data, you should implement this similar to the provided PLOP noprint.php sample (included in the PLOP package)

Rainer
  • 36
  • 1
  • Thanks, I will confer with my boss if this is how he wants to proceed. This way makes more sense, considering we already generate PDFs using the PDFlib API. Thanks! – Matt Oct 04 '12 at 12:57
1

All the PHP, and other language, documentation can be found here

Taken directly from one of those pages...

<?php
/* $Id: decrypt.php,v 1.10 2011/02/23 18:51:35 rjs Exp $
 * PDFlib PLOP: PDF Linearization, Optimization, Protection
 * decryption sample in PHP
 */
/* parameters for the input document */
$in_filename = "PLOP-datasheet-encrypted.pdf";
$in_password = "DEMO";

/* parameters for the output document */
$out_filename = "";
$out_master = "";
$out_user = "";
$permissions = "";

/* This is where input files live. Adjust as necessary. */
$searchpath = "../data ../../data";

try{
    $optlist = "";

    /* create a new PLOP object */
    $plop = new PLOP();

    $optlist = sprintf("searchpath={%s}", $searchpath);
    $plop->set_option($optlist);

    /* open protected input file with the password */
    $optlist = sprintf("password {%s} ", $in_password);
    if (!($doc = $plop->open_document($in_filename, $optlist))) {
    die("Error: " . $plop->get_errmsg());
    }

    /* create the output file */
    $optlist = sprintf("masterpassword {%s} userpassword {%s} permissions {%s}", $out_master, $out_user, $permissions);
    if (!$plop->create_file($out_filename, $optlist)) {
        die("Error: " . $plop->get_errmsg());
    }

    $buf = $plop->get_buffer();
    $len = strlen($buf);

    header("Content-type: application/pdf");
    header("Content-Length: $len");
    header("Content-Disposition: inline; filename=decrypt.pdf");
    print $buf;

    /* close input and output files */
    $plop->close_document($doc);
}
catch (PLOPException $e) {
    die("PLOP exception occurred in decrypt sample:\n" .
    "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
    $e->get_errmsg() . "\n");
}
catch (Exception $e) {
    die($e);
}

$plop = 0;

?>
Justin Wood
  • 9,941
  • 2
  • 33
  • 46
  • @Matt go to http://www.pdflib.com/download/plop-and-plop-ds/, select the download for your OS... and look at the docs... It is all there... It took me three seconds to do a Google search... and don't downvote because you can't be bothered to do a little research... – Justin Wood Oct 03 '12 at 21:06
  • I did the research. I've been staring at the manual for the past week. This is not how it's done. I need to be able to do this programmatically, ***not*** via the command line. – Matt Oct 03 '12 at 21:12
  • I just linked you a website that has examples... download the package and look at the examples... – Justin Wood Oct 03 '12 at 21:13
  • I feel like a fool; I couldn't find the examples. Thanks for this. It's not the example I need, but it pointed me in the right direction. – Matt Oct 03 '12 at 22:23