使用php在本地存储中保存html页面

时间:2018-04-22 06:58:29

标签: php html pdf-to-html

我正在使用 PDFTOHTML (一个php库)将pdf文件转换为html并且它工作正常但它在浏览器中显示转换后的文件并且不存储在本地文件夹中,我想存储转换后的html在使用与pdf同名的php的本地文件夹中,即 mydata.pdf mydata.html 将pdf转换为html的代码是: -

 <?php
// if you are using composer, just use this
include 'vendor/autoload.php';

 $pdf = new \TonchikTm\PdfToHtml\Pdf('cv.pdf', [
     'pdftohtml_path' => 'C:/wamp64/www/new/poppler-0.51/bin/pdftohtml.exe',
    'pdfinfo_path' => 'C:/wamp64/www/new/poppler-0.51/bin/pdfinfo.exe'
]);

// get content from all pages and loop for they
foreach ($pdf->getHtml()->getAllPages() as $page) {
    echo $page . '<br/>';
}
?>

2 个答案:

答案 0 :(得分:1)

只需将你的foreach改为

$filePdf = 'cv'; // your pdf filename without extension
$pdf = new \TonchikTm\PdfToHtml\Pdf($filePdf.'.pdf', [
    'pdftohtml_path' => 'C:/wamp64/www/new/poppler-0.51/bin/pdftohtml.exe',
    'pdfinfo_path' => 'C:/wamp64/www/new/poppler-0.51/bin/pdfinfo.exe'
]);

$counterPage = 1;
foreach ($pdf->getHtml()->getAllPages() as $page) {
    $filename = $filePdf . "_" . $counterPage.'.html'; // set as string directory and filename where you want to save it

    if (file_exists($filename)) {
        // if file exist do something
    } else {
        // else 
        $fileOpen = fopen($filename, 'w+');
        fputs($fileOpen, $page);
        fclose($fileOpen);
    }
    $counterPage++;
    echo $page . '<br/>';
}

这将创建您的文件,例如:example_1.html,example_2.html等。 如果这对你没有帮助,那么你可能需要将file_put_contents用于ob_start()和ob_get_contents()read more here

答案 1 :(得分:0)

看看这个:

<?php
// if you are using composer, just use this
include 'vendor/autoload.php';
$pdf = new \TonchikTm\PdfToHtml\Pdf('cv.pdf', ['pdftohtml_path' => 'C:/wamp64/www/new/poppler-0.51/bin/pdftohtml.exe', 'pdfinfo_path' => 'C:/wamp64/www/new/poppler-0.51/bin/pdfinfo.exe']);
// get content from all pages and loop for they
$file = fopen('cv.html', 'w+');
$data = null;
foreach ($pdf->getHtml()->getAllPages() as $page) {
    $data .= "".$page."<br/>";
}
fputs($file, $data);
fclose($file);

我没有测试此代码