0

I have Print.js configured and it prints a file path (.pdf) just fine, but I wanted to make the onclick print a variable. Is this possible?

default: button type="button" onclick="printJS('docs/printjs.pdf') Print PDF

but instead something like: button type="button" onclick="printJS('echo $variable') Print PDF

I need it to call a different PDF to print each time based on a newly generated pdf filename. I'm using FPDF merge which merges html form field values into a PDF template and spits out a new filled PDF. I then use Print.js to call the newly created PDF and print it. But it's a new file each time...so I need to insert a variable somehow. Any ideas?

1 Answers1

1

Yes, you can pass dynamically created pdf files to your printJS function. However, the code you wrote isn't properly mixing PHP and Javascript.

Without knowing more about your environment, it's hard to help. But let's say you are just writing javascript and html, in a .php file. You may be able to fix your code like this:

button type="button" onclick="printJS('<?php echo $variable; ?>')

However, you could also create a custom javascript function to use with onClick:

<script
  function getFileAndPrint() {
    // Generate pdf and get its filename  
    // Assuming your are writing this in a .php file  
    let printFile = '<?php echo $variable; ?>'  

    // Freedom to do anything else you may want to
    // ...

    // Pass pdf to printJS
    printJS(printFile)
  }
</script>

Then trigger the function on a button click event:

button type="button" onclick="getFileAndPrint()

crabbly
  • 5,106
  • 1
  • 23
  • 46