【问题标题】:HTML (js inside) page to pdf using HTML2PDF issue使用 HTML2PDF 问题将 HTML(内部 js)页面转换为 pdf
【发布时间】:2018-03-20 08:46:52
【问题描述】:

当我想使用 HTML2PDF 将 html 页面更改为 pdf 时遇到了麻烦。实际上,我不知道我的代码发生了什么。但错误看起来像这样:

要清楚,这是代码:

<?php
session_start();
ob_start();
include_once("config.php"); 
?>
<html xmlns="http://www.w3.org/1999/xhtml"> <!-- Bagian halaman HTML yang akan konvert -->
<head>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php echo $judul; ?></title>
</head>
<body>
<?php 
echo "<h1>".$judul."</h1>"; 
?>
	   <div id="chart_div"></div>
		 
  
<script type="text/javascript">    
	google.charts.load('current', {packages:["orgchart"]});
      google.charts.setOnLoadCallback(drawChart);

      function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Name');
        data.addColumn('string', 'Manager');https://jsfiddle.net/klometerz/zyq1rp81/#run
        data.addColumn('string', 'ToolTip');

        // For each orgchart box, provide the name, manager, and tooltip to show.
        data.addRows([
		
		<?php 
		include "inc/koneksi.php";
		$data = mysql_query("select * from t_chart");
		while($d = mysql_fetch_array($data))
		{
			echo "[{v:'".$d[id_chart]."', f:'".$d[f_chart];?></a><div style="color:red; size:11px; font-style:italic"><?php echo "<a href=edit_chart.php?id=".$d[id_chart].">".$d[batch_chart]."</a></div>'},'".$d[manager_chart]."', '".$d[tooltip_chart]."'],";
		}
		?>
        
        ]);

        // Create the chart.
        var chart = new google.visualization.OrgChart(document.getElementById('chart_div'));
        // Draw the chart, setting the allowHtml option to true for the tooltips.
        chart.draw(data, {allowHtml:true});
      }
	  </script>
	  
	  <?php
echo $isi;
echo "<p align='right'>".$penulis."</p>";
?>
</body>
</html>
<?php
$content = ob_get_clean();
	$content = '<page style="font-family: freeserif">'.nl2br($content).'</page>';
	require_once(dirname(__FILE__).'/html2pdf/html2pdf.class.php');
	try
	{
		$html2pdf = new HTML2PDF('L','A4','en', false, 'ISO-8859-15',array(30, 0, 20, 0));
		$html2pdf->setDefaultFont('Arial');
		$html2pdf->writeHTML($content, isset($_GET['vuehtml']));
		$html2pdf->Output('SO');
	}
	catch(HTML2PDF_exception $e) { echo $e; }
?>

请问有人可以帮我解决这个问题吗?我将不胜感激。谢谢。

【问题讨论】:

标签: javascript php html html-to-pdf


【解决方案1】:

您发布的错误消息表明 HTML2PDF 库不支持 html 中的脚本标签。您可以使用正则表达式从 html 中删除脚本标签。例如:

$text = preg_replace("/<script[^>]*?>.*?<\/script>/si", "", $content);

上面的行可以添加到该行之后:

$content = ob_get_clean();

以上代码取自:http://php.net/manual/en/function.preg-replace.php#76297

【讨论】:

  • 这不起作用,我与变量 $text 有什么关系?
  • 脚本标签导致生成 pdf 出现问题。所以它需要被移除。我发布的代码从页面内容中删除了脚本标签。变量 $text 包含当前页面的内容,没有脚本标签。在writeHTML方法中可以使用它来生成pdf
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-13
  • 1970-01-01
相关资源
最近更新 更多