最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - mpdf ignoring javascript - Stack Overflow

programmeradmin8浏览0评论

I have a question here..

I have used buffering to get the pdf files using mpdf..

but I got a little confused here..

I have some javascript on the page that I was buffering.. but the generated content from javascript is not getting converted into pdf..

let me get this straight..

lets say I have this element

<div id='text'></div>

and on my page i have a javascript code to add text to that div.. and the results is...

[open page via browser]

<div id='text'>TEXT I ADDED USING JAVASCRIPT</div>

but when i convert it to pdf...

<div id='text'></div> (I DIDNT GET THAT TEXT)

This is the script that is used.. template.php

ob_start();

// content of page (included inline javascript code)

$yourfile = ob_get_content();
$_SESSION[forms] = $yourfile;
echo $yourfile;

convert.php

session_start();
include("MPDF54/mpdf.php");

$mpdf=new mPDF('c','A4','','' , 0 , 0 , 0 , 0 , 0 , 0); 

$mpdf->SetDisplayMode('fullpage');

$mpdf->list_indent_first_level = 0; // 1 or 0 - whether to indent the first level of a list

$mpdf->WriteHTML($_SESSION[forms]);

$mpdf->Output();

Anybody can help me with this???

I have a question here..

I have used buffering to get the pdf files using mpdf..

but I got a little confused here..

I have some javascript on the page that I was buffering.. but the generated content from javascript is not getting converted into pdf..

let me get this straight..

lets say I have this element

<div id='text'></div>

and on my page i have a javascript code to add text to that div.. and the results is...

[open page via browser]

<div id='text'>TEXT I ADDED USING JAVASCRIPT</div>

but when i convert it to pdf...

<div id='text'></div> (I DIDNT GET THAT TEXT)

This is the script that is used.. template.php

ob_start();

// content of page (included inline javascript code)

$yourfile = ob_get_content();
$_SESSION[forms] = $yourfile;
echo $yourfile;

convert.php

session_start();
include("MPDF54/mpdf.php");

$mpdf=new mPDF('c','A4','','' , 0 , 0 , 0 , 0 , 0 , 0); 

$mpdf->SetDisplayMode('fullpage');

$mpdf->list_indent_first_level = 0; // 1 or 0 - whether to indent the first level of a list

$mpdf->WriteHTML($_SESSION[forms]);

$mpdf->Output();

Anybody can help me with this???

Share Improve this question edited Nov 5, 2019 at 9:34 tru.d 5752 gold badges7 silver badges26 bronze badges asked Dec 12, 2012 at 17:39 Its WawanIts Wawan 5996 gold badges15 silver badges27 bronze badges 1
  • I'd guess it doesn't support JavaScript. – ceejayoz Commented Dec 12, 2012 at 18:21
Add a ment  | 

2 Answers 2

Reset to default 12

mpdf supports basic javascript.

PDF readers understands basic javascript functions.

like print(); you can add it using SetJS() function of mpdf library.

include("MPDF54/mpdf.php");

$mpdf=new mPDF('c','A4','','' , 0 , 0 , 0 , 0 , 0 , 0); 

$mpdf->SetDisplayMode('fullpage');

$mpdf->list_indent_first_level = 0; // 1 or 0 - whether to indent the first level of a list

$mpdf->WriteHTML($_SESSION[forms]);

$mpdf->SetJS('print();')  //JS code with <script></script> tags.

$mpdf->Output();

PHP *PDF libraries (tcpdf, fpdf, mpdf etc...) cannot run javascript code, because that would require javascript interpreter and browser rendering capabilities, or if it supports javascript it will not run it the way you are used to using it in client side programming. Along with that, css and markup interpretation is also very poor.

However, if you are looking for alternatives that are capable of rendering javascript and css like a normal browser would, i suggest wkhtmltopdf.

wkhtmltopdf is shell utility to convert html to pdf using the webkit rendering engine, and qt. It is run as a console program with parameters specifying what to render and has tons of options. Basically you can tell it to load a page wait 5 seconds for javascript to plete then create pdf. There are linux and windows versions available, though for linux it requires X11 library to be installed.

I'm sure there are some other options out there, but I am using wkhtmltopdf for years and it produces PDF file looking exactly as the webpage would look in Chrome or Safari browsers, and also javascript execution works perfectly.

发布评论

评论列表(0)

  1. 暂无评论