How to use wkhtmltopdf with POST data

I am currently using wkhtmltopdfwhere I am trying to generate a file .pdfafter a user submits a form to our website. The form data is sent to post.php, where it is well displayed as a formatted web page. I want to create a .pdf file of this exact page.

But the problem starts when you try to execute wkhtmltopdf. I get a continuous loop because I'm trying to create a .pdf from inside this file post.php, which is also the target.

I also tried a include()separate file PHPto process the function exec(), but I still get a continuous loop.

Perhaps the picture below shows:

form.phpwhich contains something like below ...

<form id="ecard" action="post.php" method="post">
    <input type="text" name="ecard_message" />
    <input type="submit" />
</form>

post.phpwhich contains published data and contains HTMLhow it is ...

    <div class="content">
        <?php echo $_POST['ecard_message']; ?>
    </div>

    <?php exec("wkhtmltopdf http://example.com/post.php ecard.pdf"); ?>

My code works when a function exec()runs separately from these files, but how can I execute exec()it automatically in the same process?

Any insight is greatly appreciated.

+4
source share
1 answer

the call wkhtmltopdf http://example.com/post.php ecard.pdfwill lose mail data, so even if it works, the pdf file will be empty.

Rather generate pdf as html and then pass it to wkhtmltopdf. For example: (unverified)

<?php
  $html = '<div class="content">' . $_POST['ecard_message'] . '</div>';
  exec("echo $html | wkhtmltopdf - ecard.pdf");
?> 

See Is there a wkhtmltopdf option for converting HTML text, not a file? to explain the use of text instead of files.

0
source

Source: https://habr.com/ru/post/1658552/


All Articles