dompdf:模板和$ _SESSION变量 [英] dompdf: templates & $_SESSION variables

查看:65
本文介绍了dompdf:模板和$ _SESSION变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这种设置是否可行。我必须从一堆变量中提取出一批PDF,然后通过表格(duh ...)推送到$ _SESSION中。想法是将模板文件传递给dompdf引擎,并从$ _SESSION填充模板,然后将其填充为PDF。在我看来,当$ template加载时,应该这样做吗?

i was wondering if this setup would work. i have to crank out a batch of PDF from a bunch of variables i'm pushing into the $_SESSION via a form (duh...). the idea is to pass the template file to the dompdf engine and have the template populate from the $_SESSION then out to PDF. it seems to me that when the $template gets loaded it should do that, yes?

这是基本代码:

<?php
function renderToPDF($theTemplate = "template.php") // this is just to show the value
{
  require_once("dompdf/dompdf_config.inc.php");

  $content = file_get_contents($theTemplate); 

  if ($content !== false)
  {
    $dompdf = new DOMPDF();
    $dompdf->load_html($content);
    $dompdf->render();
    $dompdf->stream("kapow_ItWorks.pdf");
  }
}
?>

这是 template.php 文件(基本上...您不希望全部16页...)

and this is the template.php file (basically... you don't want all 16 pages...)

<html>
<meta>
<head>
  <link href="thisPage.css" type="text/css" rel="stylesheet">
</head>
  <body>
    <h1><?php echo $_SESSION['someTitle'] ?></h1>
    <h2>wouldn't it be nice, <?php echo $_SESSION['someName'] ?></h2>
  </body>
</html>

所以我的想法是template.php会将变量直接从<$ c $中拉出c> $ _ SESSION 数组,无需任何干预,看起来像这样:

so my thinking is that the template.php will pull the variables right out of the $_SESSION array without any intervention, looking like this:

我想问题的关键是: $ _ SESSION 变量在加载PHP文件但未呈现时得到评估吗?

i guess the nut of the question is: Do $_SESSION variables get evaluated when PHP files are loaded, but not rendered?

WR!

推荐答案

由于某种原因,调用函数ALSO的页面上的代码会转储到文件中。这被放置在标题之前。我现在了解原因:我不是引用外部页面,而是导入和外部页面。不知道为什么没有点击。

for some reason, the code ON the page that calls the function ALSO gets dumped into the file. this was placed before the header. i understand now why: i wasn't referencing an external page, i was importing and external page. don't know why that didn't click.

无论如何。一旦我摆脱了该页面的多余内容,它就可以正常工作。回想起来,dompdf需要声明的内容非常简单,即在调用该函数的页面上不能存在任何类型的HTML(echo,print和& c。)。

anyway. as soon as i got rid of the page's extra stuff, it worked just fine. in retrospect, what dompdf needed to state was quite simply that NO HTML of ANY kind (echo, print, &c.) can be on the page that calls the function. at least that what it appears to require at this level of my knowledge.

对于那些像我一样挣扎着一切皆有答案的困惑的人,这是完成工作的基本代码:

for those who, like me, are floundering in a misma of 'everything but the answer', here's the bare bones code that did the job:

buildPDF.php:

<?php
session_start();
$_SESSION['someTitle'] = "BIG FAT TITLE";
$_SESSION['someName'] = "HandomeLu";

$theTemplate = 'template.php';

function renderToPDF($templateFile)
{
  require_once("_dox/dompdf/dompdf_config.inc.php");
  ob_start();
  include $templateFile;
  $contents = ob_get_clean(); 

  if ($contents !== false)
  {
    $dompdf = new DOMPDF();
    $dompdf->load_html($contents);
    $dompdf->render();
    $dompdf->stream("kapow_ItWorks.pdf");
 }
}

renderToPDF($theTemplate);
?>

这是 template.php:

    <!DOCTYPE HTML>
    <html>
    <meta>
    <head>
      <meta charset="utf-8">
      <link href="thisPage.css" type="text/css" rel="stylesheet">
    </head>
    <body>
      <h1><?php echo $_SESSION['someTitle'] ?></h1>
      <p>wouldn't it be nice, <?php echo $_SESSION['someName'] ?></p>
    </body>
   </html>

还请注意,外部CSS文件的读取效果很好。因此您仍然可以将结构和样式分开。同样,$ _SESSION变量可以在任何地方设置,显然,我只是在这里设置它们以使测试容易。

also note that the external CSS file reads in just fine. so you can still keep the structure and style separate. also, the $_SESSION variables can be set anywhere, obviously, i just set them here to keep testing easy.

希望这对于那些开始使用GREAT类的人很有用。如果您希望起步并运行以启动PDF文件,那么效果会非常糟糕,应该有一个触发点并加以控制。 :)

hope this is useful for those getting started with this GREAT class. if you're looking to get up and running cranking out PDF files, this kicks so much butt, it should have a trigger and a grip on it. :)

感谢所有发表评论的人。你让我到了我需要成为的地方。 :)

thanks to everyone who commented. you got me in the place i needed to be. :)

此站点令人震惊。

WR!

这篇关于dompdf:模板和$ _SESSION变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆