使用dompdf使用动态变量值渲染html

时间:2018-06-05 08:14:26

标签: php html pdf dompdf fat-free-framework

我正在尝试将值传递给模板视图文件中的变量,并使用 public void LoadGeneric(IQueryable<Person> queryable, string relations) { var splitted = relations.Split('.'); var actualType = typeof(Person); IQueryable actual = queryable; foreach (var property in splitted) { actual = LoadSingleRelation(actual, ref actualType, property); } MethodInfo enumerableToListMethod = typeof(Enumerable).GetMethod("ToList", BindingFlags.Public | BindingFlags.Static); var genericToListMethod = enumerableToListMethod.MakeGenericMethod(new[] { actualType }); var results = genericToListMethod.Invoke(null, new object[] { actual }); } private IQueryable LoadSingleRelation(IQueryable queryable, ref Type actualType, string property) { var origType = actualType; var prop = actualType.GetProperty(property, BindingFlags.Instance | BindingFlags.Public); var reflectedType = prop.PropertyType; actualType = reflectedType; var isGenericCollection = reflectedType.IsGenericType && reflectedType.GetGenericTypeDefinition() == typeof(ICollection<>); MethodCallExpression selectExpression; if (isGenericCollection) { var selectMethod = typeof(Queryable).GetMethods().Single(a => a.Name == "SelectMany" && a.GetGenericArguments().Length == 2 && a.MakeGenericMethod(typeof(object), typeof(object)).GetParameters()[1].ParameterType == typeof(Expression<Func<object, IEnumerable<object>>>)); var par = Expression.Parameter(origType, "x"); var propExpr = Expression.Property(par, property); var lambda = Expression.Lambda(propExpr, par); var firstGenType = reflectedType.GetGenericArguments()[0]; //TODO: why do I get an exception here? selectExpression = Expression.Call(null, selectMethod.MakeGenericMethod(new Type[] {origType, firstGenType}), new Expression[] { queryable.Expression, lambda}); } else { var selectMethod = typeof(Queryable).GetMethods().Single(a => a.Name == "Select" && a.MakeGenericMethod(typeof(object), typeof(object)).GetParameters()[1].ParameterType == typeof(Expression<Func<object, object>>)); var par = Expression.Parameter(origType, "x"); var propExpr = Expression.Property(par, property); var lambda = Expression.Lambda(propExpr, par); selectExpression = Expression.Call(null, selectMethod.MakeGenericMethod(new Type[] {origType, reflectedType}), new Expression[] {queryable.Expression, lambda}); } var result = Expression.Lambda(selectExpression).Compile().DynamicInvoke() as IQueryable; return result; } 加载html。

但是传递的值不会被评估和渲染。

这是我的控制器功能:

this.$store.state.cookieAgreement // This returns your value

this.$store.state('cookieAgreement') // This returns error since .state is not a function its an object

模板文件(invoice.html):

dompdf

这是呈现的内容:

    $dompdf = new Dompdf();
    $this->f3->set('user_name', 'Ross Geller');
    $this->f3->set('total_amount_due', 2270.00);
    $this->f3->set('amount_received', 1000.00);
    $this->f3->set('remaining_amount', 1270.00);
    $this->f3->set('received_by',  'Rakesh Mali');
    $this->f3->set('received_on', '2018-06-05 06:00:00');

    $template = new \View;
    $html =  $template->render('lunch/invoice.html');
    $dompdf->loadHtml($html);

// (Optional) Setup the paper size and orientation
    $dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
    $dompdf->render();

// Output the generated PDF to Browser
    $dompdf->stream();

如何使用设置的值加载html?

1 个答案:

答案 0 :(得分:1)

包含html模板文件,但是作为PHP文件..然后使用$代替@

在PHP脚本中引用变量

示例

    $dompdf = new Dompdf();
    $user_name = 'Ross Geller';
    $total_amount_due = 2270.00;

    ob_start();
    require('invoice_template.php');
    $html = ob_get_contents();
    ob_get_clean();
    $dompdf->loadHtml($html);

// (Optional) Setup the paper size and orientation
    $dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
    $dompdf->render();

// Output the generated PDF to Browser
    $dompdf->stream();

然后在invoice_template.php文件中有以下内容;

<label class="control-label col-sm-2" for="email">A/C Payee:</label>
<div class="col-sm-3">
    <?=$user_name?>
</div>

<label class="control-label col-sm-2" for="pwd">Pending Total Amount:</label>
<div class="col-sm-10"> 
    Rs. <span class="amount_holder"><?=$total_amount_due?></span>
</div>