尝试下载"Hello world"使用JS和PHP [英] Trying to download "Hello world" using JS and PHP

查看:133
本文介绍了尝试下载"Hello world"使用JS和PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击链接时,我正在尝试下载pdf.

I'm trying to download a pdf when i click a link.

这些是我的代码:

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的检查器,请在控制台的响应"部分中获得以下信息:

When i click on the link, it doesn't download the pdf document. If i go the inspector of Firebug i get this below in the "Response" section of the Console:

%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不会有任何问题.

What should i do to download the document? I don't have any problem to download the pdf if i call directly issueBill() method.

推荐答案

您不想发出AJAX请求,因为响应存储在JavaScript变量中,所以您想发出普通的HTTP请求-当某人单击链接或在浏览器地址栏中键入URL时进行的.

You don't want to make an AJAX request, where the response is stored in a JavaScript variable, you want to make a normal HTTP request -- the kind made when someone clicks a link or types a URL in the browser address bar.

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

Send the user to the URL of your PDF generator with window.location in JavaScript.

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

If you want to try to trick the browser into staying on your page while it does it, make the request by creating an iframe pointing to the URL of your PDF generator. You'll also have to fudge the content-type header to NOT tell the browser it's a PDF, otherwise it might attempt to display it in the window instead of forcing a download.

这篇关于尝试下载"Hello world"使用JS和PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆