【问题标题】:Enable to generate pdf (It give me plan html text in a file) - dompdf启用生成pdf(它给我计划文件中的html文本) - dompdf
【发布时间】:2021-12-26 06:09:19
【问题描述】:
$content['profile_data'] = file_get_contents(base64_decode($profile_data,true));
$pdf = PDF::loadHtml(htmlentities($content['profile_data']));
$pdf->setOptions(['setIsHtml5ParserEnabled'=>true]);
// $pdf->setPaper('A4','landscape');
$pdf->stream();
$pdf->save($path . '/' . $file_name.'.pdf');

整个代码看起来没问题。来自 url 的 html。我正在尝试将其保存为 pdf 格式的文件。

但是当我尝试打开它时,它给了我一个计划文本 HTML。请帮帮我

谢谢

【问题讨论】:

    标签: php pdf pdf-generation wkhtmltopdf dompdf


    【解决方案1】:

    您不渲染 (->render())。

    简短示例:

    $html = ''; // Your html.
    $size = 'A4';
    $orientation = 'portrait';
    $options = new Options(
        [
            'isHtml5ParserEnabled'    => true, // little faster
        ]
    );
    $domPdf = new Dompdf($options);
    $domPdf->loadHtml($html);
    $domPdf->setPaper($size, $orientation);
    $domPdf->render();
    $pdf = $domPdf->output();
    

    我在这里发布我使用 dompdf 所做的所有事情 -
    我搜索了很多,很多边做边学......也许有帮助:

    以下适用于版本v0.8.6

    /**
     * Returns pdf from html.
     *
     * @param string $html
     * @param string $size
     * @param string $orientation
     *
     * @return string
     */
    public function htmlToPdf($html, $size = 'A4', $orientation = 'portrait')
    {
        $options = new Options(
            [
                //'logOutputFile'           => 'data/log.htm',
                'isPhpEnabled'            => false,
                'isRemoteEnabled'         => false,
                'isJavascriptEnabled'     => false,
                'isHtml5ParserEnabled'    => true, // little faster
                'isFontSubsettingEnabled' => false,
                'debugPng'                => false,
                'debugKeepTemp'           => false,
                'debugCss'                => false,
                'debugLayout'             => false,
                'debugLayoutLines'        => false,
                'debugLayoutBlocks'       => false,
                'debugLayoutInline'       => false,
                'debugLayoutPaddingBox'   => false,
                //'pdfBackend'              => 'CPDF',
            ]
        );
        $domPdf = new Dompdf($options);
        $domPdf->loadHtml($this->minimizeHtml($html));
        $domPdf->setPaper($size, $orientation);
        $domPdf->render();
        return $domPdf->output();
    }
    
    /**
     * Minimizes the html source.
     *
     * @see http://stackoverflow.com/a/6225706/3411766
     *
     * @param string $html
     *
     * @return string
     */
    public function minimizeHtml($html)
    {
        return preg_replace(
            [
                '/\>[^\S ]+/s',  // strip whitespaces after tags, except space
                '/[^\S ]+\</s',  // strip whitespaces before tags, except space
                '/(\s)+/s'       // shorten multiple whitespace sequences
            ],
            [
                '>',
                '<',
                '\\1'
            ],
            $html
        );
    }
    

    【讨论】:

    • 你是对的@cotton,但这是问题所在。这是我的 html [www2.pcrecruiter.net/pcrbin/… 我希望将其存储为文件。当我使用您的代码时,我收到此错误 Frame not found in cellmap 请帮助我研究了很多没有发现。链接 html 我无法更改其中的任何内容。
    • 如果我这样做 htmlentities(file_get_contents($url_above,true)); 它会存储但给我一个纯 html 作为 pdf 文件中的文本。我不知道如何正确生成它。请帮忙
    • $content['profile_data'] = htmlentities(file_get_contents(base64_decode($profile_data,true))); $html = $content['profile_data']; $options = new Options( ['isHtml5ParserEnabled' =&gt; true, // little faster]); $domPdf = new Dompdf($options); $domPdf-&gt;loadHtml($html); $domPdf-&gt;setPaper($size, $orientation); $domPdf-&gt;render(); $pdf = $domPdf-&gt;output(); file_put_contents($path . '/' . $file_name.'.pdf', $pdf);
    • 如果您在 HTML 上使用htmlentities,那么我不知道 dompdf 是否可以处理它。我也不知道我为什么要加载外部 HTML。可能存在安全风险。如果可能的话,至少你可以在 dompdf 中禁用 JS 和 PHP(isJavascriptEnabledisPhpEnabled 为 false)。
    • 我明白,但实际上,我需要为我在 PCR 中拥有的数据创建一个 pdf。 PCR 给我基本码,解码后给我一个链接。所以然后我必须使用该链接来制作 PDF。这就是我卡住的地方:|
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 2014-01-07
    • 2011-01-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多