简体   繁体   中英

wkhtmltopdf/perl: HTTP headers & logging

I just discovered wkhtmltopdf and I'm trying to use it in a perl CGI script to generate PDFs. Basically, the perl script writes an HTML file, calls wkhtmltopdf via system() to create a pdf, then downloads the pdf and deletes the temporary files.

open NNN, ">$path_to_files/${file}_pdf.html" or die "can't write file: $!";
print NNN $text;
close NNN;

my @pdfSettings = (
    "d:/very/long/path/wkhtmltopdf",
    "$path_to_files/${file}_pdf.html",
    "$path_to_files/$file.pdf"
    );

system(@pdfSettings);

open(DLFILE, '<', "$path_to_files/$file.pdf");
   print $q->header(
        -type=> 'application/x-download',
        -attachment => "$file.pdf",
        -filename => "$file.pdf",
        'Content-length' => -s "$path_to_files/$file.pdf",
);

binmode DLFILE;
print while <DLFILE>;
close (DLFILE);


unlink("$path_to_files/${file}_pdf.html");
unlink("$path_to_files/${file}.pdf");

This works fine on my local server. However, when I upload it to my public server, it gets as far as creating the pdf file and then dies with "The specified CGI application misbehaved by not returning a complete set of HTTP headers."

Moving the "print $q->header" to before the system() call causes the pdf to generate with wkhtmltopdf's console output ("Loading pages (1/6)," etc.) at the top of the file, so I think what's happening is that wkhtmltopdf is spewing that information headerless to the server and causing it to fail. But I can't find any options in the wkhtmltopdf docs to turn off the console output, and I can't figure out a perl method to suppres/redirect that output.

(Yes, I'm aware of WKHTMLTOPDF.pm, but I was having trouble installing it for my flavor of ActivePerl and I wanted to avoid switching if possible.)

通过qx或反引号而不是system()执行,然后将输出重定向到NUL怎么样?


qx("d:/very/long/path/wkhtmltopdf" "$path_to_files/${file}_pdf.html" "$path_to_files/$file.pdf" > NUL: 2> NUL:);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM