1

除了将文件转换Poppler为. 我通过安装该库,不幸的是,除了一些有关安装的指南外,没有可用的文档,但仍然不完整。PDFHTMLcomposer

鉴于此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