1

私は現在、ファイルをに変換するために、このライブラリhttps://github.com/php-poppler/php-popplerを使用しています。経由でライブラリをインストールしましたが、残念ながら、インストールに関するいくつかのガイドを除いて利用可能なドキュメントはありませんが、まだ不完全です。PopplerPDFHTMLcomposer

これを考えるとMain API usage

$file = new Poppler\Process\PdfFile(...);

// Get pdf info
print_r($file->getInfo('test.pdf'));

// Get text content of pdf
echo $file->toText('test.pdf');

// Transform to html
$file->toHtml('test.pdf', '/path/for/html');

どのパラメーターを指定する必要があるかを定義することさえできません$file = new Poppler\Process\PdfFile(...);

私が試したこと:

<?php 
include 'vendor/autoload.php';
use Poppler\Processor\PdfFile;

use Poppler\Driver\Pdfinfo;
use Poppler\Driver\Pdftohtml;
use Poppler\Driver\Pdftotext;

$a = new Pdfinfo;
$b = new Pdftohtml;
$c = new Pdftotext;

$file = new PdfFile($a,$b,$c);

print_r($file->getInfo('test.pdf'));
echo $file->toText('test.pdf');

$file->toHtml('test.pdf', 'Results');

?>

これによりエラーが発生します。

Catchable fatal error: Argument 1 passed to Alchemy\BinaryDriver\AbstractBinary::__construct() must be an instance of Alchemy\BinaryDriver\ProcessBuilderFactoryInterface, none given

PdfFile.php は次のとおりです。

<?php

namespace Poppler\Processor;

use Poppler\Driver\Pdfinfo;
use Poppler\Driver\Pdftohtml;
use Poppler\Driver\Pdftotext;
use Poppler\Exception\FileNotFoundException;

class PdfFile
{

    private $pdfinfo;
    private $pdftotext;
    private $pdftohtml;

    public function __construct(Pdfinfo $pdfinfo, Pdftotext $pdftotext, Pdftohtml $pdftohtml)
    {
     $this->pdfinfo = $pdfinfo;
     $this->pdftotext = $pdftotext;
     $this->pdftohtml = $pdftohtml;
    }

    public function toText($inputfile, $toEncoding = 'UTF-8')
    {
        if (!file_exists($inputfile)) {
            throw new FileNotFoundException("File $inputfile not found.");
        }

        $output = $this->pdftotext->command(array('-nopgbrk', $inputfile, '-'));
        $fromEncoding = mb_detect_encoding($output);
        if ($fromEncoding) {
            return mb_convert_encoding($output, $toEncoding, $fromEncoding);
        }

        return mb_convert_encoding($output, $toEncoding);
    }

    public function toHtml($inputfile, $outputfile)
    {
        if (!file_exists($inputfile)) {
            throw new FileNotFoundException("File $inputfile not found.");
        }

        $output = $this->pdftohtml->command(array($inputfile, $outputfile));

        return $output;
    }


    public function getInfo($inputfile)
    {
        if (!file_exists($inputfile)) {
            throw new FileNotFoundException("File $inputfile not found.");
        }

        $args = array($inputfile);

        $output = $this->pdfinfo->command($args);

        $info = array();
        foreach (explode(PHP_EOL, $output) as $line) {
            if (strpos($line, ': ') === false) {
                continue;
            }
            $parts = explode(': ', $line);
            $key = trim($parts[0]);
            $value = trim($parts[1]);
            $info[$key] = $value;
        }

        return $info;
    }
}
4

0 に答える 0