How to generate pdf with .sh using php?

by wilmer.lemke , in category: PHP General , a year ago

How to generate pdf with .sh using php?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by hal.littel , a year ago

@wilmer.lemke 

To generate a PDF using a .sh script in PHP, you can use the exec function to execute the .sh script from PHP. Here's an example of how you can do that:

  1. Create a shell script (e.g., generate_pdf.sh) that contains the code to generate the PDF. The script should output the PDF file to the standard output.
1
2
3
4
5
6
7
#!/bin/bash
# generate_pdf.sh

# Code to generate the PDF (e.g., using wkhtmltopdf or any other tool)
# The output should be directed to standard output

echo "This is the content of the PDF" | wkhtmltopdf - output.pdf


Make sure the generate_pdf.sh file has executable permissions (chmod +x generate_pdf.sh).

  1. In your PHP code, use the exec function to run the .sh script and capture any output:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php
// your_php_file.php

// Execute the shell script and store the output
exec("./generate_pdf.sh", $output);

// Assuming that the PDF file name is 'output.pdf', you can save it or present to the user for download:
$pdfFile = "output.pdf";
// ... your code to handle the generated PDF file as needed.
?>


Keep in mind that the specific command (wkhtmltopdf in this example) and parameters in the shell script (generate_pdf.sh) may vary depending on the tool you are using to generate the PDF.

by lindsey.homenick , a year ago

@wilmer.lemke 

Additionally, make sure that the shell script (generate_pdf.sh) and the PHP file (your_php_file.php) are in the same directory.


You also need to ensure that the necessary dependencies and tools (e.g., wkhtmltopdf) are installed on your server to generate PDF files.


It's important to note that executing shell scripts from PHP can be a security risk if not handled properly. Make sure to validate any user input and sanitize the commands to prevent any potential security vulnerabilities.