【问题标题】:Error with DomPdf render html in phpDomPdf 在 php 中渲染 html 时出错
【发布时间】:2013-02-27 21:59:02
【问题描述】:

我尝试使用 div、tables、thead、tboody、tfoot、h2、h3 和 ccs 呈现视图 HTML。

我用donpdf在php中渲染的结果是:

"%PDF-1.3 1 0 obj << /Type /Catalog /Outlines 2 0 R /Pages 3 0 R >> endobj 2 0 obj << /Type /Outlines /Count 0 >> endobj 3 0 obj << /Type /Pages /Kids [6 0 R ] /Count 1 /Resources << /ProcSet 4 0 R /Font << /F1 8 0 R >> >> /MediaBox [0.000 0.000 612.000 792.000] >> endobj 4 0 obj [/PDF /Text ] endobj 5 0 obj << /Creator (DOMPDF) /CreationDate (D:20130227215320+00'00') /ModDate (D:20130227215320+00'00') >> endobj 6 0 obj << /Type /Page /Parent 3 0 R /Contents 7 0 R >> endobj 7 0 obj << /Filter /FlateDecode /Length 66 >> stream x��2�300P@&�ҹ�B�M��́����BH����������BH��B���H�f�B���k�� endstream endobj 8 0 obj << /Type /Font /Subtype /Type1 /Name /F1 /BaseFont /Times-Roman /Encoding /WinAnsiEncoding >> endobj xref 0 9 0000000000 65535 f 0000000008 00000 n 0000000073 00000 n 0000000119 00000 n 0000000273 00000 n 0000000302 00000 n 0000000416 00000 n 0000000479 00000 n 0000000616 00000 n trailer << /Size 9 /Root 1 0 R /Info 5 0 R >> startxref 725 %%EOF"

知道为什么dompdf 会呈现此响应吗?

这是生成PDF的代码:

class PdfView extends PhpView {

public $orientation = "portrait";
public $size = "letter";

function render(Response $response) {
    $response->resetBuffer();
    parent::render($response);
    $buffer = utf8_decode($response->getBuffer());
    $response->resetBuffer();
    Context::load("app/core/lib/dompdf/dompdf_config.inc");
    $pdf = new DOMPDF();
    $pdf->set_paper($this->size,$this->orientation);
    $pdf->load_html($buffer);
    $pdf->render();
    $render = $pdf->output();
    if ($path = @$response->result->options->save)
        file_put_contents($path, $render);
    else {
        $response->setHeader("Content-Type", "application/pdf");
        echo $render;
    }
}

}

应用的标题是“application/pdf”。 请帮帮我!!

【问题讨论】:

  • 这看起来像 PDF。您是否发送了正确的内容类型标头?
  • @Pekka웃 是对的,这几乎是一个 PDF 文档。您可以发布您正在使用的代码吗?仅供参考,$dompdf-&gt;stream() 将为您设置适当的标题。

标签: php html dompdf


【解决方案1】:

我遇到了同样的问题。基本问题可以在类库中找到,如下:

/* FUNCION QUE DARÁ ERRORES*/
/* Function with troubles*/
if (PHP_VERSION < 6) {
    $magic_quotes = get_magic_quotes_runtime();
    set_magic_quotes_runtime(0);
}
$file_buffer = file_get_contents($path);
$file_buffer = $this->EncodeString($file_buffer, $encoding);
fclose($fd);
if (PHP_VERSION < 6) { set_magic_quotes_runtime($magic_quotes); }
    return $file_buffer;
}

你必须用这个代码替换:

/* NUEVA FUNCION CORRECTA */
/* Fuction correct*/  
if (PHP_VERSION < 6) {
    $magic_quotes = get_magic_quotes_runtime();
    ini_set("magic_quotes_runtime", 0);
}
$file_buffer = file_get_contents($path);
$file_buffer = $this->EncodeString($file_buffer, $encoding);
fclose($fd);
if (PHP_VERSION < 6) { ini_set("magic_quotes_runtime", $magic_quotes); }
    return $file_buffer;
}

希望这段代码对你有帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-15
    • 2013-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    • 2021-01-18
    相关资源
    最近更新 更多