2017 © Pedro Peláez
 

library pdftothumb

Convert PDF to an image

image

ottosmops/pdftothumb

Convert PDF to an image

  • Thursday, May 10, 2018
  • by ottosmops
  • Repository
  • 1 Watchers
  • 0 Stars
  • 25 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 14 % Grown

The README.md

Convert a PDF to an image with pdftoppm

GitHub license Scrutinizer Code Quality Code Coverage Build Status Latest Stable Version Packagist Downloads, (*1)

This package provides a wrapper for pdftoppm., (*2)

  \Ottosmops\Pdftothumb\Converter::create('/path/to/file.pdf')->convert();
  //creates a thumb of the first page: '/path/to/file.jpg'

We use this as an alternative to the excellent spatie/pdf-to-image package as we sometimes have large PDFs to convert and then it seems to be faster and more memory friendly to use pdftoppm., (*3)

Requirements

The Package uses pdftoppm. Make sure that this is installed: which pdftoppm, (*4)

For Installation see: poppler-utils, (*5)

If the installed binary is not found ("The command "which pdftoppm" failed.") you can pass the full path to the _constructor (see below) or use putenv('PATH=$PATH:/usr/local/bin/:/usr/bin') (with the dir where pdftoppm lives) before you call the class Converter., (*6)

Installation

composer require ottosmops/pdftothumb

Usage

Converting PDF to jpg:, (*7)

$exitCode = (new Converter($source, $target, $executable))->convert();

$target and $executable are optional., (*8)

Or like this:, (*9)

$converter = Converter::create($source);
$converter->convert()

You can set some options:, (*10)

Converter::create('/path/to/source.pdf')
                 ->target('/path/to/target.jpg')
                 ->executable('path/to/pdftoppm')
                 ->format('jpeg') // jpeg | png | tiff
                 ->scaleTo(150)
                 ->page(1) // or ->firstpage(1)->lastpage(1)
                 ->convert();

You can add options:, (*11)

Converter::create('/path/to/source.pdf')
                ->addOption('-gray')
                ->convert();

Or you can replace all options and set them by hand:, (*12)

Converter::create('/path/to/source.pdf')
                ->setOptions('-f 3 -l 3 -scale-to 200 -png')
                ->convert();

Default options are: -f 1 -l 1 -scale-to 150 -jpeg, (*13)

Usage for spatie/medialibrary

Tell the medialibrary not to use the standard ImageGenarator., (*14)

config/medialibrary.php, (*15)

/*
* These generators will be used to created conversion of media files.
*/
'image_generators' => [
    Spatie\MediaLibrary\ImageGenerators\FileTypes\Image::class ,
    //Spatie\MediaLibrary\ImageGenerators\FileTypes\Pdf::class ,
    Spatie\MediaLibrary\ImageGenerators\FileTypes\Svg::class ,
    Spatie\MediaLibrary\ImageGenerators\FileTypes\Video::class ,
],

Create a new ImageGenerator, (*16)

app/ImageGenarators/Pdf.php, (*17)

<?php

namespace App\ImageGenerators;

use Illuminate\Support\Collection;
use Spatie\MediaLibrary\Conversion\Conversion;
use Spatie\MediaLibrary\ImageGenerators\BaseGenerator;
use Ottosmops\Pdftothumb\Converter;

class Pdf extends BaseGenerator
{
   /**
    * This function should return a path to an image representation of the given file.
    */
    public function convert(string $path, Conversion $conversion = null) : string
    {
        $imageFile = pathinfo($path, PATHINFO_DIRNAME).'/'.pathinfo($path, PATHINFO_FILENAME).'.jpg';

        Converter::create($path)->target($imageFile)->convert();

        return $imageFile;
    }

    public function requirementsAreInstalled() : bool
    {
        return true;
    }

    public function supportedExtensions() : Collection
    {
        return collect(['pdf']);
    }

    public function supportedMimeTypes() : Collection
    {
        return collect('application/pdf');
    }
}

License

The MIT License (MIT). Please see License File for more information., (*18)

The Versions

10/05 2018

dev-master

9999999-dev https://github.com/ottosmops/pdftothumb

Convert PDF to an image

  Sources   Download

MIT

The Requires

 

The Development Requires

by ak

pdf anton ottosmops pdftothumb pdftoppm

10/05 2018

v1.0.0

1.0.0.0 https://github.com/ottosmops/pdftothumb

Convert PDF to an image

  Sources   Download

MIT

The Requires

 

The Development Requires

by ak

pdf anton ottosmops pdftothumb pdftoppm

09/05 2018

v0.9.0

0.9.0.0 https://github.com/ottosmops/pdftothumb

Convert PDF to an image

  Sources   Download

MIT

The Requires

 

The Development Requires

by ak

pdf anton ottosmops pdftothumb pdftoppm