├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml ├── phpunit.xml.dist ├── src ├── Exception │ └── RuntimeException.php └── PdfToPpm.php └── tests ├── Resources ├── test_1_page.pdf ├── test_2_pages.pdf └── test_3_pages.pdf ├── Tests └── PdfToPpmTest.php └── bootstrap.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | composer.lock 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 waarneembemiddeling 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | php-pdftoppm 2 | ============= 3 | [![Build Status](https://scrutinizer-ci.com/g/waarneembemiddeling/php-pdftoppm/badges/build.png?b=master)](https://scrutinizer-ci.com/g/waarneembemiddeling/php-pdftoppm/build-status/master) 4 | [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/waarneembemiddeling/php-pdftoppm/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/waarneembemiddeling/php-pdftoppm/?branch=master) 5 | 6 | PHP wrapper for the [pdftoppm command][1] which is part of 7 | [poppler-utils][2]. 8 | 9 | The Pdftoppm is a library that handles conversion of PDF files to images. It is available on variety of systems. 10 | 11 | Available packages: 12 | [http://pkgs.org/download/poppler-utils][3] 13 | 14 | Usage 15 | ------------- 16 | ``` 17 | use Wb\PdfToPpm\PdfToPpm; 18 | $pdfToPpm = PdfToPpm::create(); 19 | // $result is an instance of \FilesystemIterator 20 | $result1 = $pdfToPpm->convertPdf('path/to/pdf'); 21 | $result2 = $pdfToPpm->convertPdf('path/to/pdf', 'path/to/other/destination/dir/then/tmp'); 22 | 23 | // Save as png 24 | $result3 = $pdfToPpm->convertPdf('path/to/pdf', 'path/to/other/destination/dir/then/tmp', true); 25 | 26 | // Set specific resolution 27 | $result4 = $pdfToPpm->convertPdf('path/to/pdf', 'path/to/other/destination/dir/then/tmp', true, 300); 28 | 29 | ``` 30 | 31 | Testing 32 | ------------- 33 | 34 | ``` 35 | cp phpunit.xml.dist phpunit.xml 36 | ``` 37 | 38 | Change the phpunit.xml ```env``` ```binary``` directive if necessary. 39 | 40 | ``` 41 | composer install 42 | ``` 43 | 44 | ``` 45 | php vendor/bin/phpunit 46 | ``` 47 | 48 | [1]: http://linux.die.net/man/1/pdftoppm 49 | [2]: http://en.wikipedia.org/wiki/Poppler_(software) 50 | [3]: http://pkgs.org/download/poppler-utils 51 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "waarneembemiddeling/php-pdftoppm", 3 | "require": { 4 | "alchemy/binary-driver": "~1.5" 5 | }, 6 | "require-dev": { 7 | "phpunit/phpunit": "~4.0", 8 | "symfony/http-foundation": "v2.5.0" 9 | }, 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Erik van Wingerden", 14 | "email": "e.vanwingerden@waarneembemiddeling.nl" 15 | }, 16 | { 17 | "name": "Waarneembemiddeling.nl developers", 18 | "email": "development@waarneembemiddeling.nl" 19 | } 20 | ], 21 | "autoload": { 22 | "psr-4": { "Wb\\PdfToPpm\\": "src/" } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | tests 6 | 7 | 8 | 9 | 10 | src/ 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | tests 6 | 7 | 8 | 9 | 10 | src/ 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/Exception/RuntimeException.php: -------------------------------------------------------------------------------- 1 | getDestinationDir($destinationRootFolder); 57 | if (! $this->createDirectory($destinationFolder)) { 58 | throw new RuntimeException(sprintf('Destination folder "%s" could not be created', $destinationFolder)); 59 | } 60 | 61 | $options = $this->buildOptions($inputPdf, $destinationFolder, $saveAsPng, $resolution); 62 | 63 | try { 64 | $this->command($options); 65 | } catch (ExecutionFailureException $e) { 66 | throw new RuntimeException('PdfToPpm was unable to convert pdf to images', $e->getCode(), $e); 67 | } 68 | 69 | return new \FilesystemIterator($destinationFolder, \FilesystemIterator::SKIP_DOTS); 70 | } 71 | 72 | private function createDirectory($destinationFolder) 73 | { 74 | return mkdir($destinationFolder); 75 | } 76 | 77 | private function getDestinationDir($destinationRootFolder) 78 | { 79 | return $destinationRootFolder . '/' . uniqid('pdftoppm').'/'; 80 | } 81 | 82 | /** 83 | * @param bool $saveAsPng 84 | * @param $inputPdf 85 | * @param null $destinationRootFolder 86 | */ 87 | private function buildOptions($inputPdf, $destinationFolder, $saveAsPng, $resolution = null) 88 | { 89 | $options = array(); 90 | 91 | if ($saveAsPng) { 92 | $options[] = '-png'; 93 | } 94 | 95 | if ($resolution) { 96 | $options[] = '-r'; 97 | $options[] = $resolution; 98 | } 99 | 100 | $options[] = $inputPdf; 101 | $options[] = $destinationFolder; 102 | 103 | return $options; 104 | } 105 | 106 | 107 | /** 108 | * Creates the pdftoppm wrapper 109 | * 110 | * @param array|ConfigurationInterface $configuration 111 | * @param LoggerInterface $logger 112 | * 113 | * @return PdfToPpm 114 | */ 115 | public static function create($configuration = array(), LoggerInterface $logger = null) 116 | { 117 | if (!$configuration instanceof ConfigurationInterface) { 118 | $configuration = new Configuration($configuration); 119 | } 120 | 121 | $binaries = $configuration->get('pdftoppm.binaries', array('pdftoppm')); 122 | 123 | return static::load($binaries, $logger, $configuration); 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /tests/Resources/test_1_page.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waarneembemiddeling/php-pdftoppm/780098379f2852619db2b83fca4cc5cc54bdac40/tests/Resources/test_1_page.pdf -------------------------------------------------------------------------------- /tests/Resources/test_2_pages.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waarneembemiddeling/php-pdftoppm/780098379f2852619db2b83fca4cc5cc54bdac40/tests/Resources/test_2_pages.pdf -------------------------------------------------------------------------------- /tests/Resources/test_3_pages.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/waarneembemiddeling/php-pdftoppm/780098379f2852619db2b83fca4cc5cc54bdac40/tests/Resources/test_3_pages.pdf -------------------------------------------------------------------------------- /tests/Tests/PdfToPpmTest.php: -------------------------------------------------------------------------------- 1 | pdfToPpm = PdfToPpm::create(array( 24 | 'pdftoppm.binaries' => getenv('binary') 25 | )); 26 | } 27 | 28 | public function testConvertPdfOnePages() 29 | { 30 | $result = $this->pdfToPpm->convertPdf(dirname(__DIR__) . '/Resources/test_1_page.pdf'); 31 | 32 | $this->assertSame(1, iterator_count($result)); 33 | } 34 | 35 | public function testConvertPdfMultiplePages() 36 | { 37 | $result = $this->pdfToPpm->convertPdf(dirname(__DIR__) . '/Resources/test_3_pages.pdf'); 38 | 39 | $this->assertSame(3, iterator_count($result)); 40 | } 41 | 42 | public function testConvertPdfAsPpm() 43 | { 44 | $result = $this->pdfToPpm->convertPdf(dirname(__DIR__) . '/Resources/test_1_page.pdf', null); 45 | 46 | $mimeTypeGuesser = MimeTypeGuesser::getInstance(); 47 | $mimeType = $mimeTypeGuesser->guess($result->current()->getPathName()); 48 | 49 | $this->assertSame('image/x-portable-pixmap', $mimeType); 50 | } 51 | 52 | public function testConvertPdfAsPng() 53 | { 54 | $result = $this->pdfToPpm->convertPdf(dirname(__DIR__) . '/Resources/test_1_page.pdf', null, true); 55 | 56 | $mimeTypeGuesser = MimeTypeGuesser::getInstance(); 57 | $mimeType = $mimeTypeGuesser->guess($result->current()->getPathName()); 58 | 59 | $this->assertSame('image/png', $mimeType); 60 | } 61 | 62 | public function testExtractImageToGivenDir() 63 | { 64 | $destinationDir = sys_get_temp_dir() . '/pdftoppm'; 65 | @mkdir($destinationDir); 66 | 67 | $result = $this->pdfToPpm->convertPdf(dirname(__DIR__) . '/Resources/test_1_page.pdf', $destinationDir); 68 | 69 | $this->assertSame(1, iterator_count($result)); 70 | } 71 | 72 | public function testExceptionIfInvalidInputFileIsGiven() 73 | { 74 | $exception = new RuntimeException(sprintf('Input file "%s" not found', null)); 75 | try { 76 | $destinationDir = sys_get_temp_dir() . '/pdftoppm'; 77 | @mkdir($destinationDir); 78 | 79 | $this->pdfToPpm->convertPdf(null, $destinationDir); 80 | } catch (RuntimeException $expected) { 81 | $this->assertEquals($expected, $exception); 82 | return; 83 | } 84 | 85 | $this->fail('An expected exception has not been raised.'); 86 | } 87 | 88 | public function testExceptionIfDestinationRootFolderIsNotADirectory() 89 | { 90 | $destinationRootFolder = '!@#'; 91 | $exception = new RuntimeException(sprintf('Destination folder "%s" not found', $destinationRootFolder)); 92 | try { 93 | $this->pdfToPpm->convertPdf(dirname(__DIR__) . '/Resources/test_1_page.pdf', $destinationRootFolder); 94 | } catch (RuntimeException $expected) { 95 | $this->assertEquals($expected, $exception); 96 | return; 97 | } 98 | 99 | $this->fail('An expected exception has not been raised.'); 100 | } 101 | 102 | public function testExceptionIfDirIsNotWritable() 103 | { 104 | $destinationDir = sys_get_temp_dir() . '/pdftoppm'; 105 | @mkdir($destinationDir); 106 | chmod($destinationDir, '000'); 107 | 108 | $exception = new RuntimeException(sprintf('Destination folder "%s" is not writable', $destinationDir)); 109 | try { 110 | $this->pdfToPpm->convertPdf(dirname(__DIR__) . '/Resources/test_1_page.pdf', $destinationDir); 111 | } catch (RuntimeException $expected) { 112 | $this->assertEquals($expected, $exception); 113 | return; 114 | } 115 | 116 | $this->fail('An expected exception has not been raised.'); 117 | } 118 | 119 | /** 120 | * @after 121 | */ 122 | public function tearDownEnsureDirExistsAndIsWritable() 123 | { 124 | chmod(sys_get_temp_dir() . '/pdftoppm', '755'); 125 | } 126 | 127 | } 128 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 | set('Tests', __DIR__); 16 | --------------------------------------------------------------------------------