如何通过来自 autohotkey 的 WinHttpRequest post 请求下载由 dompdf 创建的 pdf

How to download pdf created by dompdf by WinHttpRequest post request from 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 脚本,它什么都不做。它给出了一个错误 'The certificate authority is invalid or incorrect'.

尝试这样的事情...我认为您混淆了服务器 headers 和客户端 headers...

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