使用PHP将HTML页面保存在本地存储中



我正在使用 pdftohtml (php库(将pdf文件转换为html,它工作正常,但是它在浏览器中显示了转换的文件,并且不存储在local中文件夹,我想使用与pdf相同的名称的PHP将转换后的HTML存储在本地文件夹中将PDF转换为HTML的代码为: -

 <?php
// if you are using composer, just use this
include 'vendor/autoload.php';
 $pdf = new TonchikTmPdfToHtmlPdf('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/>';
}
?>

只需将您的foreach更改为

$filePdf = 'cv'; // your pdf filename without extension
$pdf = new TonchikTmPdfToHtmlPdf($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/>';
}

这将创建您的文件:例如:xpuse_1.html,example_2.html等。如果这对您没有帮助,那么您可能需要使用ob_start((和ob_get_contents((使用file_put_contents((

看这个:

<?php
// if you are using composer, just use this
include 'vendor/autoload.php';
$pdf = new TonchikTmPdfToHtmlPdf('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);

我没有测试此代码

相关内容

  • 没有找到相关文章