【发布时间】:2018-02-05 04:01:30
【问题描述】:
我一直在尝试ExportPDF 使用 iTextSharp dll 文件。该代码在本地运行良好,但出现以下错误。
{Message: "That assembly does not allow partially trusted callers.",…}
ExceptionType
:
"System.Security.SecurityException"
Message
:
"That assembly does not allow partially trusted callers."
StackTrace
:
" at Admin_WebMethods.ExportToPDF(String html, String IssuedToemailID)"
这是ExportPDF(..)
[WebMethod]
public static int ExportToPDF(string html, string IssuedToemailID)
{
int status = 0;
try
{
string FileName = "Invoice_" + DateTime.Now.ToString("M_dd_yyyy_H_M_s");
html = html.Replace(">", ">");
html = html.Replace("<", "<");
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + FileName + ".pdf");
StringReader sr = new StringReader(html);
Document pdfDoc = new Document(PageSize.A4, 10f, 2f, 2f, 2f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
//PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
string path = @"~\PdfGeneration\" + FileName + ".pdf";
PdfWriter.GetInstance(pdfDoc, new FileStream(HttpContext.Current.Server.MapPath(path), FileMode.Create));
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
HttpContext.Current.ApplicationInstance.CompleteRequest();
// send email here
sendemail(path, IssuedToemailID);
status = 1;
}
catch (Exception exp)
{
status = 0;
}
return status;
}
这就是我从 JavaScript 文件调用此方法的方式
function SaveToPDF() {
$('div[id$=divStockoutPdf] > div[id$=divItems]').html($('#divStockoutItems').html());
//remove the delete button from pdf
$('div[id$=divStockoutPdf] > div[id$=divItems] table td:last').remove();
$('div[id$=divStockoutPdf] > div[id$=divItems] table th:last').remove();
var IssuedToemailID = $('#txtStockoutIssuedToEmail').val();
var contents = $('#divStockoutPdf').html(); //$('#divStockoutItems').html();
contents = contents.replace(/>/g, '>');
contents = contents.replace(/</g, '<');
$.ajax({
type: "POST",
url: "WebMethods.aspx/ExportToPDF",
data: "{'html': '" + contents + "','IssuedToemailID': '" + IssuedToemailID + "' }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
if (msg.d == 1) {
$('#btnCloseSignaure').click();
UpdateStockout();
}
else
alert("Internal Error. Kindly contact support team. Error at SaveToPDF");
},
error: function (er) { alert(er.error) }
});
}
我已经尝试了所有在线可用的解决方案,但似乎没有任何效果,或者我可能无法正确实施该解决方案。我关注了this
【问题讨论】:
-
与您的问题无关,但您不应使用
HTMLWorker,因为它已被弃用。请改用 iText 7 + pdfHTML。
标签: javascript c# .net itext