0

I have been trying to ExportPDF using the iTextSharp dll file. The code works fine locally but gives following error.

{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)"

This is 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("&lt;", "<");
            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;

    }

This is how I am calling this method from JavaScript file

  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, '&gt;');
            contents = contents.replace(/</g, '&lt;');
    
            $.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) }
            });
        }

I have tried all the solutions available online but nothing seems to be working or I might not been able to implement the solution properly. I have followed this

1
  • Unrelated to your question, but you should not use HTMLWorker because it is deprecated. Use iText 7 + pdfHTML instead. Feb 5, 2018 at 15:41

1 Answer 1

2

looks like you explicitly need to make ISharpText assembly to allow partially trusted users. Below link might help :

https://www.aspsnippets.com/Articles/ASPNet-iTextSharp-SystemSecuritySecurityException-That-assembly-does-not-allow-partially-trusted-callers.aspx

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.