将Web服务响应转换为PDF文件-PHP、Javascript

将Web服务响应转换为PDF文件-PHP、Javascript,javascript,php,jquery,pdf,download,Javascript,Php,Jquery,Pdf,Download,这是从服务器下载文档的服务: $app->get('/docDownload', function () use ($app){ $path = "/xxx/xxx/works.pdf"; $res = $app->response(); $res['Content-Description'] = 'File Transfer'; $res['Content-Type'] = 'application/pdf';

这是从服务器下载文档的服务:

$app->get('/docDownload', function () use ($app){

        $path = "/xxx/xxx/works.pdf";
        $res = $app->response();
        $res['Content-Description'] = 'File Transfer';
        $res['Content-Type'] = 'application/pdf';
        $res['Content-Disposition'] ='attachment; filename=' . basename($path);
        $res['Content-Transfer-Encoding'] = 'binary';
        $res['Expires'] = '0';
        $res['Cache-Control'] = 'must-revalidate';
        $res['Pragma'] = 'public';
        $res['Content-Length'] = filesize($path);
        readfile($path);
    });
这就是答案

%PDF-1.5
%����
1 0 obj
<</Type/Catalog/Pages 2 0 R/Lang(en-GB) /StructTreeRoot 14 0 R/MarkInfo<</Marked true>>>>
endobj
2 0 obj
<</Type/Pages/Count 1/Kids[ 3 0 R] >>
endobj
3 0 obj
<</Type/Page/Parent 2 0 R/Resources<</Font<</F1 5 0 R/F2 8 0 R>>/ExtGState<</GS7 7 0 R>>/ProcSet[/PDF
/Text/ImageB/ImageC/ImageI] >>/MediaBox[ 0 0 595.32 841.92] /Contents 4 0 R/Group<</Type/Group/S/Transparency
/CS/DeviceRGB>>/Tabs/S/StructParents 0>>
endobj
4 0 obj
<</Filter/FlateDecode/Length 224>>
stream
x���Kk�0����9J�����`|�� �@����J�C
M!��$��bA,�ш]T�h�j��V0]���r���0��8R0L.F����70�3�}�8\�08L�V�Q��+�')��g��U;��V
��8�o�����o��Ip�I}�W_�r}��N'mգU��g>Ö�Ӎ���n>�D��.�-����<H`��ABC\ǐ'�=��ٻXwc�z��wx�
endstream
endobj
5 0 obj
<</Type/Font/Subtype/TrueType/Name/F1/BaseFont/ABCDEE+Calibri/Encoding/WinAnsiEncoding/FontDescriptor
 6 0 R/FirstChar 32/LastChar 119/Widths 24 0 R>>
endobj
6 0 obj
<</Type/FontDescriptor/FontName/ABCDEE+Calibri/Flags 32/ItalicAngle 0/Ascent 750/Descent -250/CapHeight
 750/AvgWidth 521/MaxWidth 1743/FontWeight 400/XHeight 250/StemV 52/FontBBox[ -503 -250 1240 750] /FontFile2
 22 0 R>>
endobj
...... continues ........
在JQuery中是否有其他方法可以做到这一点


WS-response是什么类型的?

对我来说,您可以使用
jQuery.ajax()
这样做:

$.ajax({
url:“/documents/docDownload”,
键入:“获取”,
标题:{
响应类型:“blob”
},
成功:功能(数据){

var file=window.URL.createObjectURL(数据); 变量a=$(“”{ “href”:文件, “下载”:data.name | |“detailPDF” }).附于(“主体”); a、 单击(); $(窗口).on('focus',函数(e){ $('a')。删除(); }); } })
如果可以用javascript实现这一点,为什么要使用jQuery?真的!如果这对你有用的话,那么就坚持下去,让另一个库来处理同样的问题对我来说不是一个好主意,但要回答这个问题,是的,你也可以用jQuery来完成。所有其他使用此web服务的平台都在使用jQuery来发出ajax请求,如果可能的话,我想保留这个模式在jquery中我在哪里可以定义responseType=“blob”?在尝试运行代码后,我得到了这个错误“TypeError:参数1对于URL.createObjectURL的任何两个参数重载都无效。
window.URL.createObjectURL(this.response)
js版本的输出是什么?您可以发布它吗?或者您可以澄清这一行实际上在做什么吗?var file=window.URL.createObjectURL(数据);
var request = new XMLHttpRequest();
    request.open("GET", "/documents/docDownload", true); 
    request.responseType = "blob";
    request.onload = function (e) {
        if (this.status === 200) {
            // `blob` response
            //console.log(this.response);
            // create `objectURL` of `this.response` : `.pdf` as `Blob`
            var file = window.URL.createObjectURL(this.response);
            var a = document.createElement("a");
            a.href = file;
            a.download = this.response.name || "detailPDF";
            document.body.appendChild(a);
            a.click();
            // remove `a` following `Save As` dialog, 
            // `window` regains `focus`
            window.onfocus = function () {                     
              document.body.removeChild(a)
            }
        };
    };
    request.send();