[英]FPDF - password protected pdf not downloading
I am trying to generate a password protected pdf using my php
script.我正在尝试使用我的
php
脚本生成受密码保护的 pdf 。 This is my code:这是我的代码:
JS : 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 :
<?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();
}
}
?>
But, when I run the code an alert pops up showing showing this:但是,当我运行代码时,会弹出一个警报,显示如下:
%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]
%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
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
I don't understand, why the pdf isn't downloading.我不明白,为什么 pdf 没有下载。 Please help me.
请帮我。
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);
This should force the file to download.这应该强制文件下载。
I changed the code a bit for the demo, but the concept remains the same, here: https://jsfiddle.net/ntmwks9q/我为演示更改了一些代码,但概念保持不变,这里: https://jsfiddle.net/ntmwks9q/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.