【发布时间】:2016-03-31 17:00:19
【问题描述】:
在 php 中创建 pdf 作为字符串,如下所示:
%PDF-1.4
(..skipping...)
/F1 9 Tf
0 g
BT
157 830 Td
(Lorem ) Tj
ET
(..skipping...)
8 0 obj
<</Type /Font /Encoding /WinAnsiEncoding /Subtype /Type1 /BaseFont /Helvetica >>
endobj
我的php代码:
//string it's pdf as plain text
return new Response($string);
在 js 中我使用 ajax:
$("a#get_pdf").on('click', function(e){
e.preventDefault();
$.ajax('/get_pdf/');
});
我如何下载这个文件,因为我有纯文本的响应,不喜欢一个文件。
-----编辑-----
我可以这样做,就像在这个答案中一样: https://stackoverflow.com/a/34065784/4900669
-----编辑 2----
但经过一番搜索后,我编辑了我的 js 代码(https://stackoverflow.com/a/20830337/4900669):
$("a#get_pdf").on('click', function(e){
e.preventDefault();
$.ajax({
url: '/get_pdf/',
type: 'POST',
success: function() {
window.location = '/get_pdf/';
}
});
});
而且效果很好。
【问题讨论】: