ai didi

php - 尝试使用 JS 和 PHP 下载 "Hello world"

转载 作者:行者123 更新时间:2023-11-30 18:48:06 24 4
gpt4 key购买 nike

我尝试在单击链接时下载 pdf。

这些是我的代码:

function issue_bill(order_id){
$.post('orders/issueBilll?id_order='+order_id,
order_id,
function()
{
$('example').replaceWith('Bill issued');
}
);
}


public function executeIssueBill(sfWebRequest $request)
{
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
}

当我单击链接时,它不会下载 pdf 文档。如果我去 Firebug 的检查员,我会在控制台的“响应”部分看到下面的内容:

%PDF-1.3
3 0 obj
<</Type /Page
/Parent 1 0 R
/Resources 2 0 R
/Contents 4 0 R>>
endobj
4 0 obj
<</Filter /FlateDecode /Length 72>>
stream
x�3R��2�35W(�r
Q�w3T04�30PISp
�Z*�[����(hx����+���(j*�d��
endstream
endobj
1 0 obj
<</Type /Pages
/Kids [3 0 R ]
/Count 1
/MediaBox [0 0 595.28 841.89]
>>
endobj
5 0 obj
<</Type /Font
/BaseFont /Helvetica-Bold
/Subtype /Type1
/Encoding /WinAnsiEncoding
>>
endobj
2 0 obj
<<
/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]
/Font <<
/F1 5 0 R
>>
/XObject <<
>>
>>
endobj
6 0 obj
<<
/Producer (FPDF 1.53)
/CreationDate (D:20110126103602)
>>
endobj
7 0 obj
<<
/Type /Catalog
/Pages 1 0 R
/OpenAction [3 0 R /FitH null]
/PageLayout /OneColumn
>>
endobj
xref
0 8
0000000000 65535 f
0000000228 00000 n
0000000416 00000 n
0000000009 00000 n
0000000087 00000 n
0000000315 00000 n
0000000520 00000 n
0000000596 00000 n
trailer
<<
/Size 8
/Root 7 0 R
/Info 6 0 R
>>
startxref
699
%%EOF

下载文档应该怎么做?如果我直接调用 issueBill() 方法,我下载 pdf 没有任何问题。

最佳答案

您不想发出 AJAX 请求,其中响应存储在 JavaScript 变量中,您想要发出普通的 HTTP 请求——当有人单击链接或在浏览器地址中键入 URL 时发出的那种请求酒吧。

使用 JavaScript 中的 window.location 将用户发送到您的 PDF 生成器的 URL。

如果您想尝试诱使浏览器停留在您的页面上,请通过创建一个指向您的 PDF 生成器 URL 的 iframe 来发出请求。您还必须捏造内容类型 header 以不告诉浏览器它是 PDF,否则它可能会尝试在窗口中显示它而不是强制下载。

关于php - 尝试使用 JS 和 PHP 下载 "Hello world",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4803191/

24 4 0
文章推荐: javascript - jQuery .after() 在 IE 中不工作
文章推荐: C# 循环限制为 50 遍
文章推荐: C#:用户代码未处理 ArgumentOutOfRangeException
文章推荐: javascript - 从未指定的 anchor 标记获取 HREF
行者123
个人简介

我是一名优秀的程序员,十分优秀!

滴滴打车优惠券免费领取
滴滴打车优惠券
全站热门文章
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com