首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PHP将PDF转换为HTML

使用PHP将PDF转换为HTML
EN

Stack Overflow用户
提问于 2015-07-09 15:37:34
回答 4查看 11.2K关注 0票数 0

我需要转换一些pdf文件为HTML。我下载了PHP版的pdftohtml,但我不知道如何使用它。我试着用下面的代码运行它:

代码语言:javascript
复制
<?php  
    include 'pdf-to-html-master/src/Gufy/PdfToHtml.php';
    $pdf = new \Gufy\PdfToHtml;
    $pdf->open('1400.pdf');
    $pdf->generate();
?>

这会导致一个空白的网页。

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

EN

回答 4

Stack Overflow用户

发布于 2015-07-09 15:55:16

第一个选项是使用弹出器实用程序

代码语言:javascript
复制
<?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();
?>

Download library from here的第二个选项可能是使用pdf.js

代码语言:javascript
复制
PDFJS.getDocument('helloworld.pdf')
票数 1
EN

Stack Overflow用户

发布于 2015-10-28 08:00:37

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

票数 0
EN

Stack Overflow用户

发布于 2020-05-10 13:14:44

代码语言:javascript
复制
include 'vendor/autoload.php';

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

公共函数parsepdf(请求$request) {

代码语言:javascript
复制
    $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需要gufy/pdftohtml-php:~2

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31311004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档