1

我需要将一些 pdf 文件转换为 HTML。我为 PHP 下载了 pdftohtml,但我不知道如何使用它。我正在尝试使用以下代码运行它:

<?php  
    include 'pdf-to-html-master/src/Gufy/PdfToHtml.php';
    $pdf = new \Gufy\PdfToHtml;
    $pdf->open('1400.pdf');
    $pdf->generate();
?>

这会导致一个空白网页。

我需要修改什么?运行此脚本的正确代码是什么?

4

4 回答 4

2

第一个选项是使用 poppler utils

<?php
// if you are using composer, just use this
include 'vendor/autoload.php';
// if not, use this
include 'src/Gufy/PdfToHtml.php';
// initiate 
$pdf = new \Gufy\PdfToHtml;
// opening file
$pdf->open('file.pdf');
// set different output directory for generated html files
$pdf->setOutputDirectory('/your/absolute/directory/path');
// do this if you want to convert in the same directory as file.pdf
$pdf->generate();
// you think your generated files is annoying? simple do this to remove the whole files
$pdf->clearOutputDirectory();
?>

从这里下载库 第二个选项可能是使用pdf.js

PDFJS.getDocument('helloworld.pdf')
于 2015-07-09T07:55:15.793 回答
0
include 'vendor/autoload.php';

use Gufy\PdfToHtml\Pdf;
    use PHPHtmlParser\Dom;
    use DateTime;

公共函数解析pdf(请求$请求){

    $pdf = new Pdf($request->file('csv_file'));
    $html = $pdf->html();
    $dom = new Dom;
    $total_pages = $pdf->getPages();

    if ($total_pages == 1) {
        $html->goToPage(1);            
        $dom->load($html);
        $paragraphs = $dom->find('p');
        $paragraphs = collect($paragraphs);
        foreach($paragraphs as $p){
           $datestring = preg_replace('/\xc2\xa0/', ' ', trim($p->text));
           echo $datestring;
        }
  }

以上代码在 laravel 中将 pdf 转换为 html

composer require gufy/pdftohtml-php:~2

Poppler-Utils(如果您使用的是 Ubuntu Distro,只需从 apt 安装它) sudo apt-get install poppler-utils

于 2020-05-10T05:14:43.710 回答
0

我是包的维护者。包已更新。你已经用过最新版本了吗?而且,如果您使用的是 Windows,请再次阅读文档。另外,请不要直接从github下载,而是使用composer。

于 2015-10-28T00:00:37.440 回答
-2

我使用 wkhtmltopdf,它工作正常。你可以从这里下载它:http ://wkhtmltopdf.org/downloads.html

我将它安装在 Linux 中并像这样使用它:

$url = "https://www.google.com";

$command = "/usr/bin/wkhtmltopdf --load-error-handling ignore --disable-smart-shrinking -T 5mm -B 5mm -L 2mm -R 2mm  --page-size Letter --encoding utf-8 --quiet";

$filename = '[file path].pdf';
if (file_exists($filename)) {
   unlink($filename);
}

$output = shell_exec($command . " $url " . $filename);

echo $output;

希望这可以帮助。

于 2015-07-09T07:54:10.657 回答