【问题标题】:dompdf, Store the Contents of a Wordpress Page in a Variabledompdf,将 Wordpress 页面的内容存储在变量中
【发布时间】:2014-01-18 21:35:42
【问题描述】:

使用 WordPress 3.8,我包含了 dompdf 库来生成我的网站的投资组合,我尝试了不同的方式,但没有得到解决方案。这是我尝试过的代码。

<?php
require_once ('dompdf/dompdf_config.inc.php');
$id="80";
$post = get_page($id);
$contents = apply_filters('the_content', $post->post_content); 

$html =
  '<html><body>'.
  '<p>Put your html here, or generate it with your favourite '.
  'templating system.</p>'; 
  $html.= $contents;
  $html .='</body></html>';

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("Dotcom Portfolio.pdf");
}
?>

不是:当我回显 $contents 变量时,它会打印存储在其中的数据。就像

$post = get_page($id);
$contents = apply_filters('the_content', $post->post_content); 

echo $contents;

die();

但不存储在 $html 变量中转换为 PDF。

提前致谢。

【问题讨论】:

  • 你能告诉我$content变量包含哪些数据吗?
  • 它包含整个数据,动态WordPress页面包含的意思是页面的所有内容
  • 是的,但我需要看看它的 html 是什么样子。所以请死$contents并将它的值复制粘贴到这里。
  • 太长的结果无法在评论中显示,当我打印 $content 时,它包含图片和图表以及文本数据。
  • 当您尝试创建 PDF 时会向浏览器发送什么?如果你回显$html 变量,你会得到什么?

标签: php wordpress dompdf


【解决方案1】:

试试这个步骤

    ob_start();
    include('./some_dynamic_page.php');
    $content = ob_get_clean();

    require_once("dompdf/dompdf_config.inc.php");

    $dompdf = new DOMPDF();
    $dompdf->load_html($content);
    $dompdf->set_paper('A4', 'portrait');
    $dompdf->render();
    $dompdf->stream("result.pdf", array("Attachment" => 0));

还有配置: (dompdf_config.custom.inc)

//define("DOMPDF_TEMP_DIR", "/tmp"); //define("DOMPDF_CHROOT", DOMPDF_DIR); 定义(“DOMPDF_UNICODE_ENABLED”,真); //define("DOMPDF_PDF_BACKEND", "PDFLib"); 定义(“DOMPDF_DEFAULT_MEDIA_TYPE”,“打印”); 定义(“DOMPDF_DEFAULT_PAPER_SIZE”,“A4”); //define("DOMPDF_DEFAULT_FONT", "serif"); 定义(“DOMPDF_DPI”,300); 定义(“DOMPDF_ENABLE_PHP”,真); 定义(“DOMPDF_ENABLE_REMOTE”,真); 定义(“DOMPDF_ENABLE_CSS_FLOAT”,真); //define("DOMPDF_ENABLE_JAVASCRIPT", false); //define("DEBUGPNG", true); //define("DEBUGKEEPTEMP", true); //define("DEBUGCSS", true); //define("DEBUG_LAYOUT", true); //define("DEBUG_LAYOUT_LINES", false); //define("DEBUG_LAYOUT_BLOCKS", false); //define("DEBUG_LAYOUT_INLINE", false); //define("DOMPDF_FONT_HEIGHT_RATIO", 1.0); //define("DEBUG_LAYOUT_PADDINGBOX", false); //define("DOMPDF_LOG_OUTPUT_FILE", DOMPDF_FONT_DIR."log.htm"); 定义(“DOMPDF_ENABLE_HTML5PARSER”,真); 定义(“DOMPDF_ENABLE_FONTSUBSETTING”,真); // DOMPDF 认证 定义(“DOMPDF_ADMIN_USERNAME”,“管理员”); 定义(“DOMPDF_ADMIN_PASSWORD”,“任何密码”);

【讨论】:

  • some_dynamic_page.php 将是:post_content); ?>

    把你的html放在这里,或者用你最喜欢的模板系统生成它。

  • 也可以不使用“include”直接包含完整代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-02
  • 1970-01-01
  • 1970-01-01
  • 2017-04-08
  • 1970-01-01
  • 2011-05-12
相关资源
最近更新 更多