Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print a generated pdf via embedded javascript #713

Closed
jameswilson opened this issue Sep 9, 2013 · 5 comments
Closed

Print a generated pdf via embedded javascript #713

jameswilson opened this issue Sep 9, 2013 · 5 comments

Comments

@jameswilson
Copy link

It appears that it may be technically possible to embed some javascript inside the generated pdf that could automatically load the pdf viewer's print dialog upon load.

Is there any way to embed javascript into the pdf with DOMPDF, or can this only be done if you have implemented your own web-based PDF Viewer?

References:

@bsweeney
Copy link
Member

bsweeney commented Sep 9, 2013

You're in luck. dompdf (as of v0.6.0) has the ability to embed JavaScript into a PDF. The JS functionality is somewhat limited in that you can't tie the JS to events. But in the case of telling the PDF to print you can just tack the necessary JS to the end of the document.

First, set DOMPDF_ENABLE_JAVASCRIPT to true.

Then place the JS that calls the print dialog at the bottom of your HTML document:

<html>

<head>
  <title>Javascript test</title>
</head>

<body>
  <h1>PDF JS Test</h1>
  <p>This page will call the print dialog on load.</p>
  <script type="text/javascript">
  try {
    this.print();
  }
  </script>
</body>

</html>

@aminemrabet
Copy link

To make it work, you need to use this JS bloc inside your PDF Blade :

<script type="text/javascript"> try { this.print(); } catch (e) { window.onload = window.print; } </script>

I hope it helps !

@ChaitaliSheth
Copy link

ChaitaliSheth commented Nov 16, 2018

Thank you this script was working fine

@JesustheLord
Copy link

Thank you so much. Worked for me too.

@ShekhSaifuddin007
Copy link

<script type="text/javascript"> try { this.print(); } catch (e) { window.onload = window.print; } </script>
this line go directly in print window but I have other script for showing total amount in words but this pdf generator totally skip those lines. What should I do..??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants