4

I am converting the html to pdf using wkhtmltopdf and conversion is working completely fine using below code:

shell_exec('wkhtmltopdf http://www.example.com/Haryana.htm Haryana.pdf');

Now, suppose there are some DIV's hidden in html file. Say,

In Haryana.htm

<div style="display:none;">Hello</div>

so, the word 'Hello' will also not print in the converted PDF(which is obvious).

What I need to do is to show all the hidden text to the converted PDF.

What I tried till now is, created a JS file say external-js.js which has code to show all the hidden DIV's

var elements = document.getElementsByTagName('DIV')
for (var i = 0; i < elements.length; i++){
    if (elements[i].style.display == 'none') {
           elements[i].style.display = 'block';
    }
}

and also applying this JS file while creating a PDF like below:

shell_exec('wkhtmltopdf --enable-javascript --run-script /var/www/html/search/external-js.js http://www.example.com/Haryana.htm Haryana.pdf');

Still, PDF is converting fine but hidden text are not visible.

Please help me to show all the hidden text in pdf. I hope i am able to clear the question.

Note: I can't make changes in the html files because we have thousands of files like this.

UPDATE : When added debug-javascript option and running the following code in putty

wkhtmltopdf --debug-javascript --enable-javascript --run-script /var/www/html/search/external-js.js http://www.example.com/Haryana.htm Haryana.pdf;

i am getting following output:

Loading pages (1/6)
Warning: undefined:0 SyntaxError: Invalid flags supplied to RegExp constructor.
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done

which have some warning. I am sure this warning is coming because of --run-script /var/www/html/search/external-js.js but why? I have added this as per the official documentation.

I am stuck with this...can anybody please help me out?

2
  • I think you should add a funktion in the HTML files, like page numbering function. And based on wheter the document finds the number of a page in it or not, your div`s change the style to "style.display = 'block'". You can alter all html files you have at once with Java. Commented Apr 15, 2016 at 9:09
  • I can't modify the HTML files because same file is also used for rendering in browser. Commented Apr 15, 2016 at 9:24

1 Answer 1

3

Finally, after done lots of R&D I get the solution :-). I was on the right track and what i did is instead of passing the JS script url, i just passed the whole script content to wkhtmltopdf.

Here is the code:

$script = 'var elements = document.getElementsByTagName(\'DIV\');
           for (var i = 0; i < elements.length; i++)
           {
            if (elements[i].style.display == \'none\') 
            {
            elements[i].style.display = \'block\';
            }
           }'; 

shell_exec('wkhtmltopdf --run-script "'.$script.'" http://www.example.com/Haryana.htm Haryana.pdf');

That's it :-)

Might be some other user can face same problem in future so, i am posting this as answer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.