GithubHelp home page GithubHelp logo

baikunz / slurpy Goto Github PK

View Code? Open in Web Editor NEW
8.0 5.0 9.0 290 KB

Slurpy is a PHP wrapper for the pdftk command-line tool for working with PDF.

Home Page: http://slurpy.shuble.com/

License: MIT License

PHP 100.00%

slurpy's Introduction

Slurpy

Slurpy is a PHP wrapper for the pdftk command-line tool for working with PDF. This library is largely inspired by Snappy from KnpLabs, a library for generating images or pdf from html. Some of the Slurpy code comes directly from Snappy.

In order to use Slurpy you will have to download pdftk for either Windows, Mac OSX or linux.

Build Status

Installation

If you are using composer add the following lines to your composer.json:

{
    "require" :  {
        "shuble/slurpy": "*"
    }
}

And run these commands:

wget http://getcomposer.org/composer.phar
php composer.phar install

Now, just add the autoloader:

<?php

require 'vendor/autoload.php';

Or, if you are not using composer, require the provided autoload:

<?php

require 'path/to/slurpy/src/autoload.php';

Then proceed with the installation of pdftk if not already installed. Please refer to the pdftk website, and grab the installer or sources, depending on your system.

That's it, you're done.

Simple usage

Visit the pdftk documentation for more details about each operations.

Create the factory

Slurpy comes with a simple factory for dealing with pdftk operations in their simple forms. Each call to factory methods returns a Slurpy instance, then you just need to call the generate method on this Slurpy instance to process the operation.

<?php

// Create a new factory instance, give it your path to pdftk binary
$factory = new \Shuble\Slurpy\Factory('/path/to/pdftk');

$slurpy = $factory->generateFdf('path/to/input.pdf', 'path/to/output.fdf');

$slurpy->generate();

Catenate PDF files

The cat operation assembles pages from input PDFs to create a new PDF. Use cat to merge PDF pages or to split PDF pages from documents. You can also use it to rotate PDF pages. Page order in the new PDF is specified by the order of the inputs array.

<?php

use Shuble\Slurpy\Operation\OperationArgument\PageRange;

$inputs = array(
    '/path/to/file1.pdf',
    '/path/to/file2.pdf',
    array(
        'filepath'   => '/path/to/file3.pdf',
        'password'   => 'pa$$word',
        'start_page' => 1,
        'end_page'   => 'end',
        'qualifier'  => PageRange::QUALIFIER_ODD,
        'rotation'   => PageRange::ROTATION_EAST,
    )
);

$output = '/path/to/output.pdf';

$slurpy = $factory->cat($inputs, $output);

Now, /path/to/ouput.pdf contains the 3 pdfs, with only odd pages rotated to east for the third pdf.

Shuffle PDF files

The shuffle operation collates pages from input PDFs to create a new PDF. Works like the cat operation except that it takes one page at a time from each page range to assemble the output PDF. If one range runs out of pages, it continues with the remaining ranges. This feature was designed to help collate PDF pages after scanning paper documents.

<?php

use Shuble\Slurpy\Operation\OperationArgument\PageRange;

$inputs = array(
    '/path/to/file1.pdf',
    '/path/to/file2.pdf',
    array(
        'filepath'   => '/path/to/file3.pdf',
        'password'   => 'pa$$word',
        'start_page' => 1,
        'end_page'   => 'end',
        'qualifier'  => PageRange::QUALIFIER_ODD,
        'rotation'   => PageRange::ROTATION_EAST,
    )
);

$output = '/path/to/output.pdf';

// Creates a Slurpy instance
$slurpy = $factory->shuffle($inputs, $output);

Background

The background operation Applies a PDF watermark to the background of a single input PDF. It uses only the first page from the background PDF and applies it to every page of the input PDF. This page is scaled and rotated as needed to fit the input page. If the input PDF does not have a transparent background (such as a PDF created from page scans) then the resulting background won’t be visible β€” use the stamp operation instead.

You can also pass a fourth parameter $multi if you want to use the multibackground operation.

multibackground is the same as the background operation, but applies each page of the background PDF to the corresponding page of the input PDF. If the input PDF has more pages than the stamp PDF, then the final stamp page is repeated across these remaining pages in the input PDF.

<?php

$input = '/path/to/input.pdf'; // or array('filepath' => '/path/to/input.pdf', 'password' => 'S3cr3t');
$background = '/path/to/background.pdf';
$output = '/path/to/output.pdf';
$multi = false // [Default], Or true for multibackground operation

// Creates a Slurpy instance
$slurpy = $factory->background($input, $background, $output, $multi);

Burst

The burst operation splits a single, input PDF document into individual pages. Naming for the resulting page is specified if a printf-style format string. i.e. if you give an ouput of page_%02d.pdf to Slurpy, resulting pages are going to be page_01.pdf, page_02.pdf and so on.

NOTE that you may have to give output as a complete path such as /path/to/pdfs/page_%04d.pdf

<?php

$input = '/path/to/input.pdf'; // or array('filepath' => '/path/to/input.pdf', 'password' => 'S3cr3t');
$output = '/path/to/folder/pg_%02d.pdf';

$slurpy = $factory->burst($input, $output);

Generate fdf

The generateFdf operation reads a single, input PDF file and generates an FDF file suitable for fillForm operation. It saves this FDF file using the output filename.

<?php

$input = '/path/to/input.pdf'; // or array('filepath' => '/path/to/input.pdf', 'password' => 'S3cr3t');
$output = '/path/to/folder/output.fdf';

$slurpy = $factory->generateFdf($input, $output);

Fill form

The fillForm operation fills a single input PDF form with the given fdf or xfdf data file. By default after filling the form, the ouput pdf fields remains active. You can disable the fields from the output pdf by giving true as a fourth argument which will flatten the resulting pdf.

NOTE That Slurpy will soon be able to fill forms with simple key/value pairs as data instead of fdf or xfdf files.

<?php

$input = '/path/to/input.pdf'; // or array('filepath' => '/path/to/input.pdf', 'password' => 'S3cr3t');
$data = '/path/to/data.fdf'; // or data.xfdf
$output = '/path/to/folder/output.fdf';

$slurpy = $factory->fillForm($input, $data, $output);

// Or to flatten the resulting pdf.
$slurpy = $factory->fillForm($input, $data, $output, true);

Unit tests

Run the following command:

phpunit

Done.

Credits

License

Slurpy is released under the MIT license. See the provided LICENSE file for more details.

slurpy's People

Contributors

baikunz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

slurpy's Issues

Test fail

PHP Fatal error: Class 'Shuble\Slurpy\Operation\OperationArgument\PageRange' not found in /path/here/vendor/shuble/slurpy/tests/Shuble/Slurpy/Tests/Operation/OperationArgument/PageRangeTest.php on line 49

PHP Stack trace:
PHP   1. {main}() /usr/bin/phpunit:0
PHP   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
PHP   3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:130
PHP   4. PHPUnit_Runner_BaseTestRunner->getTest() /usr/share/php/PHPUnit/TextUI/Command.php:150
PHP   5. PHPUnit_Framework_TestSuite->addTestFiles() /usr/share/php/PHPUnit/Runner/BaseTestRunner.php:96
PHP   6. PHPUnit_Framework_TestSuite->addTestFile() /usr/share/php/PHPUnit/Framework/TestSuite.php:419
PHP   7. PHPUnit_Framework_TestSuite->addTestSuite() /usr/share/php/PHPUnit/Framework/TestSuite.php:392
PHP   8. PHPUnit_Framework_TestSuite->__construct() /usr/share/php/PHPUnit/Framework/TestSuite.php:318
PHP   9. PHPUnit_Framework_TestSuite->addTestMethod() /usr/share/php/PHPUnit/Framework/TestSuite.php:214
PHP  10. PHPUnit_Framework_TestSuite::createTest() /usr/share/php/PHPUnit/Framework/TestSuite.php:831
PHP  11. PHPUnit_Util_Test::getProvidedData() /usr/share/php/PHPUnit/Framework/TestSuite.php:484
PHP  12. ReflectionMethod->invoke() /usr/share/php/PHPUnit/Util/Test.php:227
PHP  13. Shuble\Slurpy\Tests\Operation\OperationArgument\PageRangeTest->dataForGetSet() /usr/share/php/PHPUnit/Util/Test.php:227

pdftk does not like multiple character handles

Using pdftk 1.44, it seems to balk with two letter handles. A sample dump data fields command that fails.

/usr/bin/pdftk AA=my/pdf.pdf dump_data_fields output /tmp/out.txt

The documentation for pdftk indicates they should only be one letter:

"Input files can be associated with handles, where a handle is a single, upper-case letter"

new stable relase

Hi,

Is it possible to get new stable release in the near future?

Best regards,
Michal

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.