下面是我的autohotkey代码,用于将WinHttpRequest发送到php,从dompdf创建的PHP的流函数下载pdf文件。
AutoHotkey的:
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("POST", "https://spm/htmltopdf.php",false)
whr.SetRequestHeader("Content-Type","application/pdf")
whr.SetRequestHeader("Content-Disposition","attachment")
whr.SetRequestHeader("filename","file.pdf'")
whr.Send("html=" . %html%)
version := whr.ResponseText
FileAppend, %version%, *d:\cc.pdf
PHP:
<?php
header('Access-Control-Allow-Origin: *'); //to get data from firefox addon
require_once "dompdf/dompdf_config.inc.php";
$dompdf = new DOMPDF();
$html = $_POST["html"];
$dompdf->load_html($html);
$dompdf->render();
//$output = $dompdf->output();
//file_put_contents("output/file.pdf", $output);
$dompdf->stream("file.pdf");
?>
我可以通过浏览器直接导航,使用带有'http'协议的 GET 方法获取pdf。但对于这个autohotkey脚本,它什么也没做。它会出现错误'证书颁发机构无效或不正确'。
答案 0 :(得分:1)
尝试类似这样的事情......我认为您将服务器标头与客户端标头混淆......
AHK
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("POST", "https://spm/htmltopdf.php",false)
whr.SetRequestHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
whr.SetRequestHeader("Content-Type","application/x-www-form-urlencoded")
whr.Send("html=" . html)
version := whr.ResponseText
FileAppend, %version%, *d:\cc.pdf
PHP
<?php
header('Access-Control-Allow-Origin: *'); //to get data from firefox addon
header('Content-Type: application/pdf');
header('Content-Disposition: attachment');
header('filename: file.pdf');
require_once "dompdf/dompdf_config.inc.php";
$dompdf = new DOMPDF();
$html = $_POST["html"];
$dompdf->load_html($html);
$dompdf->render();
//$output = $dompdf->output();
//file_put_contents("output/file.pdf", $output);
$dompdf->stream("file.pdf");
?>
或者既然你说你的浏览器能够通过GET和http获得它,请尝试:
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("GET", "http://spm/htmltopdf.php",false)
whr.SetRequestHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
whr.Send()
version := whr.ResponseText
FileAppend, %version%, *d:\cc.pdf
或者首先使用UrlDownloadToFile:
UrlDownloadToFile, http://spm/htmltopdf.php, file.pdf