简体   繁体   中英

Password protect PDF using PLOP (PDFLib)

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?

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)

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;

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM