Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

javascript - Wkhtmltopdf doesn't create pdf file

I need to create a pdf file from html code (with tags in it) and wkhtmltopdf doesn't create a file at all. And, if does, then it's empty.

In vs code's console there is an output:

Loading pages (1/6)
[=======>                                                    ] 13%

And it lacks on this point, on 13%, without moving on. If I remove "echo" in shell_exec, then it creates an empty pdf file, and in console it loads all pages. What should I change to make it work? Thanks to anyone's reply!

My php code (I need to make a pdf file and send it with PHPMailer):

<?php

use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;
use PHPMailerPHPMailerException;

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: *');
header('Access-Control-Allow-Methods: *');
header('Content-Type: application/json');

require_once "../vendor/autoload.php";

$data = json_decode(file_get_contents('php://input'), true);
send_mail('[email protected]', 'joe brock', $data);
// I tried to console.log this parameter, and all is OK, the array is not empty.
// echo json_encode($data['html']);

function send_mail($email, $name, $data) {
    $mail = new PHPMailer(true);

    $mail->SMTPDebug = 3;
    $mail->isSMTP();
    $mail->Host = "smtp.gmail.com";
    $mail->SMTPAuth = true;
    $mail->Username = "[email protected]";
    $mail->Password = "pass"; //changed it
    $mail->SMTPSecure = "tls";
    $mail->Port = 587;

    $mail->From = "[email protected]";
    $mail->FromName = "Dealin Maivasha";

    $mail->addAddress($email, $name);

    $mail->isHTML(true);

    $mail->Subject = "Subject Text";
    // MAIN PART, which is doesn't work for some reason
    $file_name = rand(1, 100000);
    $output_path = __DIR__."/cards/$file_name.pdf";

    shell_exec("echo "" .$data['html'] . "" | wkhtmltopdf - $output_path");

    $mail->addAttachment("cards/$file_name.pdf");

    $mail->Body = "body";

    $mail->AltBody = "This is the plain text version of the email content";

    try {
        $mail->send();
        echo "Message has been sent successfully";
    } catch (Exception $e) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    }
}

My js fetch:

fetch(`${server}/dist/php/mail.php`, {
    method: "POST",
    headers: {
        "Content-Type": "application/json",
    },
    body: JSON.stringify({
        'html': document
            .querySelector(".container-order__column_card")
            .innerHTML.replaceAll('"', "'").replaceAll(/(
|
|
)/gm, ""),
    })
})
question from:https://stackoverflow.com/questions/65925696/wkhtmltopdf-doesnt-create-pdf-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...