在Magnific-Popup模式中显示PDF文件 [英] Display PDF file inside Magnific-Popup modal
问题描述
我的网站上有一个用户需要填写的表格才能生成PDF文件.如果表单中存在错误,则服务器将使用带有错误列表的json对象进行响应,否则将使用PDF文件(作为字符串)进行响应.我正在尝试在Magnific-Popup( https://www.npmjs. com/package/magnific-popup ).
I have a form on my website that the user needs to fill in order to generate a PDF file. If there are errors in the form, the server will respond with a json object with the list of errors, otherwise will respond with the PDF file (as a string). I'm trying to display that PDF file inside a Magnific-Popup (https://www.npmjs.com/package/magnific-popup).
$('.preview-button').on('click', function (e) {
event.preventDefault();
var theLink = $(this);
var formElement = theLink.closest("form");
$.ajax({
url: theLink.attr('href'),
method: 'GET',
data: formElement.serialize(),
success: function (response, textStatus, jqXHR) {
var contentType = jqXHR.getResponseHeader("content-type") || "";
if(contentType.indexOf('pdf') > -1){
// How can I tell to magnific popup to display the response as PDF?
$.magnificPopup.open({
// tell magnific popup to use the response...
type: 'iframe',
closeOnBgClick: false
});
}
else{
if (response.hasOwnProperty('errors')){
// Form has error, show them
alert(response.errors);
}
}
}
});
});
推荐答案
这对于遇到相同问题的人可能很有用:我决定不发送pdf文件作为响应,而是决定在服务器中生成一个临时pdf文件,发送该文件的路径作为响应.然后,我们可以告诉magnificPopup将其显示在iframe中.
This might be useful for someone with the same problem: Instead of sending the pdf file as a response, I decided to generate a temporary pdf file in the server and send the path to that file as response. Then we can tell to magnificPopup to display it inside an iframe.
这篇关于在Magnific-Popup模式中显示PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!