[英]FPDF - password protected pdf not downloading
我正在嘗試使用我的php
腳本生成受密碼保護的 pdf 。 這是我的代碼:
JS :
$.ajax({
url: 'backend.php',
type: 'POST',
data: {"input": "generate_pdf",
"password": document.getElementById("password").value},
success:function(response){
alert(response);
},
complete:function(){
}
});
PHP :
<?php
if(isset($_POST["input"])){
if($_POST["input"] == "sign in"){
ob_start();
require('FPDF_protection.php');
$pdf = new FPDF_Protection();
$pdf->SetProtection(array('print'), $_POST["password"]);
$pdf->AddPage();
$pdf->SetFont('Arial');
$pdf->Write(10,"Hello");
$pdf->Output('D',"Recovery_code.pdf");
ob_end_flush();
}
}
?>
但是,當我運行代碼時,會彈出一個警報,顯示如下:
%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 90>> stream Fҧ�J� 1�v"I�E�2��vE�*��q�6/~=�Y�d@ྒྷ����-������̾�M>~u�<�� �b} �f}-��b� endstream endobj 1 0 obj <</Type /Pages /Kids [3 0 R ] /Count 1 /MediaBox [0 0 595.28 841.89]
endobj 5 0 obj <</Filter /FlateDecode /Length 364>> stream �r����[�gFE��5'��Ά'oYt5��5hb%a��������Fc�2 �g����d����u��Ɉ[��u�^$,����x�<FN�%jAKz}��8���I���_3m�� u?�� }~@�y�<:>����|��ḣ���n0X�9X�0J8OG���6(����c[��E�#�R�m�ۮ��8w� -��~��n���ؿ��XϻG��^��t�8BF����������E�����֖�H �7T^�~u�A� {?�y>�csG;�xo�,��q��[J�_W)�"j���,O�7n\9�olN�8n
我不明白,為什么 pdf 沒有下載。 請幫我。
var req = new XMLHttpRequest();
var params = "input=generate_pdf&password=" + document.getElementById("password").value;
req.open("POST", "backend.php", true);
req.responseType = "blob";
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
req.onreadystatechange = function ()
{
if (req.readyState === 4 && req.status === 200)
{
var blob=new Blob([req.response]);
var link=document.createElement('a');
link.href=window.URL.createObjectURL(blob);
link.download="your_file_name_here.pdf";
link.click();
}
};
req.send(params);
這應該強制文件下載。
我為演示更改了一些代碼,但概念保持不變,這里: https://jsfiddle.net/ntmwks9q/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.