Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

php - MPDF not rendering html tags

I am using MPDF which I have installed using composer. I have some content stored in a MySQL database which includes the HTML tags as well as the text. I would like the user to be able to create and edit their own letters. When I go to print the pdf I get a PDF page with the HTML tags displayed on the page. The strange thing is; if I take that output and copy it and past it into the WriteHTML() function in the PHP page the page will render with the proper text and formatting. It doesn't seem to be an issue with the HTML markup, but how Mpdf is rendering the HTML.

Here is the $headder output before it is sent to $WriteHTML:

<h1 style="text-align: center;">Baddeck Academy Scholarships</h1> <h6     
style="text-align: center; line-height: 1;"><span style="color:         
#e67e23;">320 Shore Rd.</span></h6> <h6 style="text-align: center; 
line-height: 1;"><span style="color: #e67e23;">Baddeck, NS</span></h6> 
<h6 style="text-align: center; line-height: 1;"><span style="color: 
#e67e23;">B0E 1B0</span></h6>

Here is the exported page content as well as the code that I am using.

enter image description here

Here is the Mpdf php page:

<?php

include('connect.php');
$id = (filter_var($_GET['id'], FILTER_SANITIZE_STRING));

$sql = "SELECT * FROM `money_letters_contents` WHERE `id` = '$id' LIMIT 0,1000";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
  // output data of each row
  while($row = mysqli_fetch_assoc($result)) {
 $headder = ($row['headder']);
$body = ($row['body']);
      $footer = ($row['footer']);
  }
} else {

}

$html = $headder;

//==============================================================
//==============================================================
//==============================================================

require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new mPDF();

$mpdf->WriteHTML("$headder");
$mpdf->WriteHTML("$body");
$mpdf->Output();
exit;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok after 3 days of looking for this answer and with the help from @Finew I looked at the way that I was getting the data out of the server and I created another step where I took the mysqli $row and stored it as another variable, then I did the htmlspecialchars_decode to that variable. This seemed to work and now produces the correct result.

So for anyone looking to see what I did here is the working code.

?php



include('connect.php');
$id = (filter_var($_GET['id'], FILTER_SANITIZE_STRING));

$sql = "SELECT * FROM `money_letters_contents` WHERE `id` = '$id' LIMIT     0,1000";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
  // output data of each row
  while($row = mysqli_fetch_assoc($result)) {
 $headder = ($row['headder']);
$body = ($row['body']);
      $footer = ($row['footer']);
  }
} else {

}

$headder1 = htmlspecialchars_decode($headder);
$body1 = htmlspecialchars_decode($body);


$html = $headder;

//==============================================================
//==============================================================
//==============================================================

require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new mPDF();

$mpdf->WriteHTML("$headder1");
$mpdf->WriteHTML("$body1");
$mpdf->Output();
exit;

Thanks for everyone who looked at this question.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...