Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu Feb 12, 2015 1:53 am

Using Spire.PDF in an Azure website.

I am converting HTML into a PDF. This works locally without any issue.

When deployed to the site, I get the following error:
Application ID: /LM/W3SVC/2144249568/ROOT

Process ID: 612

Exception: System.IO.FileNotFoundException

Message: Could not load file or assembly 'Microsoft.mshtml, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

StackTrace: at Spire.Pdf.HtmlConverter.HtmlConverter.ᜀ(String A_0, ImageType A_1, Int32 A_2)
at Spire.Pdf.HtmlConverter.HtmlConverter.Convert(String url, ImageType type, Int32 width, Int32 height, AspectRatio aspectRatio)
at Spire.Pdf.PdfSection.ᜀ(String A_0, Boolean A_1, Boolean A_2, Boolean A_3, PdfHtmlLayoutFormat A_4)
at Spire.Pdf.PdfDocument.LoadFromHTML(String Url, Boolean enableJavaScript, Boolean enableHyperlinks, Boolean autoDetectPageBreak, PdfPageSettings setting, PdfHtmlLayoutFormat layoutFormat)
at Spire.Pdf.PdfDocument.LoadFromHTML(String Url, Boolean enableJavaScript, Boolean enableHyperlinks, Boolean autoDetectPageBreak)
at FSMG.Controllers.GeneratedReportsController.<>c__DisplayClass3f.<ViewPdf>b__3c()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()</Data></EventData></Event><Event><System><Provider Name=".NET Runtime"/><EventID>1026</EventID><Level>0</Level><Task>0</Task><Keywords>Keywords</Keywords><TimeCreated SystemTime="2015-02-12T01:47:00Z"/><EventRecordID>566505578</EventRecordID><Channel>Application</Channel><Computer>RD00155D50D3A9</Computer><Security/></System><EventData><Data>Application: w3wp.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.FileNotFoundException
Stack:
at Spire.Pdf.HtmlConverter.HtmlConverter.ᜀ(System.String, Spire.Pdf.HtmlConverter.ImageType, Int32)
at Spire.Pdf.HtmlConverter.HtmlConverter.Convert(System.String, Spire.Pdf.HtmlConverter.ImageType, Int32, Int32, Spire.Pdf.HtmlConverter.AspectRatio)
at Spire.Pdf.PdfSection.ᜀ(System.String, Boolean, Boolean, Boolean, Spire.Pdf.HtmlConverter.PdfHtmlLayoutFormat)
at Spire.Pdf.PdfDocument.LoadFromHTML(System.String, Boolean, Boolean, Boolean, Spire.Pdf.PdfPageSettings, Spire.Pdf.HtmlConverter.PdfHtmlLayoutFormat)
at Spire.Pdf.PdfDocument.LoadFromHTML(System.String, Boolean, Boolean, Boolean)
at FSMG.Controllers.GeneratedReportsController+&lt;&gt;c__DisplayClass3f.&lt;ViewPdf&gt;b__3c()
at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart()


I continue to get the error after adding a reference to Microsoft.mshtml, even though Embed Interop Types is set to true.

When I copy the DLL into the bin and deploy, I get the following error:

Application ID: /LM/W3SVC/2144249568/ROOT

Process ID: 13084

Exception: System.Runtime.InteropServices.COMException

Message: Error HRESULT E_FAIL has been returned from a call to a COM component.

StackTrace: at System.Windows.Forms.UnsafeNativeMethods.IWebBrowser2.Navigate2(Object&amp; URL, Object&amp; flags, Object&amp; targetFrameName, Object&amp; postData, Object&amp; headers)
at System.Windows.Forms.WebBrowser.PerformNavigate2(Object&amp; URL, Object&amp; flags, Object&amp; targetFrameName, Object&amp; postData, Object&amp; headers)
at System.Windows.Forms.WebBrowser.PerformNavigateHelper(String urlString, Boolean newWindow, String targetFrameName, Byte[] postData, String headers)
at System.Windows.Forms.WebBrowser.Navigate(String urlString, Boolean newWindow)
at Spire.Pdf.HtmlConverter.HtmlConverter.ᜆ()
at Spire.Pdf.HtmlConverter.HtmlConverter.ᜀ(String A_0, ImageType A_1, Int32 A_2)
at Spire.Pdf.HtmlConverter.HtmlConverter.Convert(String url, ImageType type, Int32 width, Int32 height, AspectRatio aspectRatio)
at Spire.Pdf.PdfSection.ᜀ(String A_0, Boolean A_1, Boolean A_2, Boolean A_3, PdfHtmlLayoutFormat A_4)
at Spire.Pdf.PdfDocument.LoadFromHTML(String Url, Boolean enableJavaScript, Boolean enableHyperlinks, Boolean autoDetectPageBreak, PdfPageSettings setting, PdfHtmlLayoutFormat layoutFormat)
at Spire.Pdf.PdfDocument.LoadFromHTML(String Url, Boolean enableJavaScript, Boolean enableHyperlinks, Boolean autoDetectPageBreak)
at FSMG.Controllers.GeneratedReportsController.&lt;&gt;c__DisplayClass3f.&lt;ViewPdf&gt;b__3c()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()</Data></EventData></Event><Event><System><Provider Name=".NET Runtime"/><EventID>1026</EventID><Level>0</Level><Task>0</Task><Keywords>Keywords</Keywords><TimeCreated SystemTime="2015-02-12T01:44:58Z"/><EventRecordID>566383796</EventRecordID><Channel>Application</Channel><Computer>RD00155D50D3A9</Computer><Security/></System><EventData><Data>Application: w3wp.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Runtime.InteropServices.COMException
Stack:
at System.Windows.Forms.WebBrowser.PerformNavigate2(System.Object ByRef, System.Object ByRef, System.Object ByRef, System.Object ByRef, System.Object ByRef)
at System.Windows.Forms.WebBrowser.PerformNavigateHelper(System.String, Boolean, System.String, Byte[], System.String)
at System.Windows.Forms.WebBrowser.Navigate(System.String, Boolean)
at Spire.Pdf.HtmlConverter.HtmlConverter.ᜆ()
at Spire.Pdf.HtmlConverter.HtmlConverter.ᜀ(System.String, Spire.Pdf.HtmlConverter.ImageType, Int32)
at Spire.Pdf.HtmlConverter.HtmlConverter.Convert(System.String, Spire.Pdf.HtmlConverter.ImageType, Int32, Int32, Spire.Pdf.HtmlConverter.AspectRatio)
at Spire.Pdf.PdfSection.ᜀ(System.String, Boolean, Boolean, Boolean, Spire.Pdf.HtmlConverter.PdfHtmlLayoutFormat)
at Spire.Pdf.PdfDocument.LoadFromHTML(System.String, Boolean, Boolean, Boolean, Spire.Pdf.PdfPageSettings, Spire.Pdf.HtmlConverter.PdfHtmlLayoutFormat)
at Spire.Pdf.PdfDocument.LoadFromHTML(System.String, Boolean, Boolean, Boolean)
at FSMG.Controllers.GeneratedReportsController+&lt;&gt;c__DisplayClass3f.&lt;ViewPdf&gt;b__3c()
at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart()



Here is the code:
Code: Select all
PdfDocument doc = new PdfDocument();
                String url = path;
                Thread thread = new Thread(() =>
                {
                    doc.LoadFromHTML(url, false, true, true);
                });
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
                thread.Join();

                if (!IsBgNull)
                {
                    var imgStr = new MemoryStream(bgImagePath);
                    var img = Image.FromStream(imgStr);
                    imgStr.Position = 0;
                    foreach (PdfPageBase page in doc.Pages)
                    {
                        page.BackgroundImage = img;
                    }
                    imgStr.Dispose();
                }

                var ms = new MemoryStream();
                doc.SaveToStream(ms, FileFormat.PDF);
                doc.Close();

patness
 
Posts: 10
Joined: Tue Jul 08, 2014 12:15 am

Thu Feb 12, 2015 9:08 am

Hello,

Thanks for your inquiry. Spire.Pdf will rely on the IE engine when html to pdf conversion, so the IE browser is essential. Please confirm if the IE browser is installed or not.
In addition, there is another method as below to convert html to pdf you can try, the method doesn't rely on the IE.
http://www.e-iceblue.com/Tutorials/Spir ... lugin.html
Sincerely,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Thu Feb 12, 2015 8:14 pm

Thank you for the response. I can invoke a webrequest in c#, which should use the IE engine. However, as this is an azure website I cannot confirm that IE is installed.

Re: the plugin, I see in the sample that the Convert method returns void. I need to save this to a memory stream so that I can save the generated doc to azure storage, as well as include it in the Response.OutputStream.

Is that possible with the plugin?

I would like to do something like this:

Code: Select all
                HtmlConverter.Convert(url, "sports.pdf", true,100*1000, new SizeF(612, 792), new PdfMargins(0, 0));
                byte[] bytes = System.IO.File.ReadAllBytes("sports.pdf");
                var ms = new MemoryStream(bytes);

patness
 
Posts: 10
Joined: Tue Jul 08, 2014 12:15 am

Fri Feb 13, 2015 6:35 am

Hello,

Thanks for your response. Sorry that the plugin only supports to save to file.
Sincerely,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Tue Feb 17, 2015 10:30 pm

Ok, thank you. For anyone interested, here is a sample asp.net mvc 5 controller that can use the plugin and can respond with the pdf. Also, I noticed the plugin doesn't retain a background image so the code below inserts one. (My actual controller saves the file to blob storage, etc... this is just to help anyone else.)

Code: Select all
        public ActionResult ViewPdf(string path)
        {
            if (User.Identity.IsAuthenticated)
            {
                var url = path;
                byte[] bgImagePath = null;
                var backgroundImage = ...;  //This would be the path to the background Image

                if (!String.IsNullOrEmpty(backgroundImage))
                {
                    bgImagePath =  ...;  controller that gets the image bytes of the background image;
                }

                var subPath = "/Content/UserFiles/TMP/TMPDOC/";

                var exists = Directory.Exists(Server.MapPath(subPath));

                if (!exists)
                    Directory.CreateDirectory(Server.MapPath(subPath));

                var fullPath = subPath + "mypdf.pdf";
                fullPath = Server.MapPath(fullPath);

                HtmlConverter.Convert(url, fullPath, true, 100 * 1000, new SizeF(612, 792), new PdfMargins(0, 0));

                using (var ms = new MemoryStream())
                {
                    using (var fs = System.IO.File.OpenRead(fullPath))
                    {
                        fs.CopyTo(ms);
                    }
                    ms.Position = 0;

                    //no need to retain document in the file system
                    System.IO.File.Delete(fullPath);

                    //Create a pdf document.
                    var doc = new PdfDocument();
                    var thread = new Thread(() =>
                    {
                        doc.LoadFromStream(ms);
                    });
                    thread.SetApartmentState(ApartmentState.STA);
                    thread.Start();
                    thread.Join();

                if (!IsBgNull)
                {
                    var imgStr = new MemoryStream(bgImagePath);
                    var img = Image.FromStream(imgStr);
                    imgStr.Position = 0;
                    foreach (PdfPageBase page in doc.Pages)
                    {
                        page.BackgroundImage = img;
                    }
                    imgStr.Dispose();
                }

                    using (var ms2 = new MemoryStream())
                    {
                        doc.SaveToStream(ms2, FileFormat.PDF);
                        doc.Close();

                        Response.ContentType = "application/pdf";
                        Response.AddHeader("Content-Disposition", "attachment; filename=" + "mypdf.pdf");
                        ms2.Position = 0;
                        ms2.CopyTo(Response.OutputStream);

                    }
                }

                return new EmptyResult();
            }
            return RedirectToAction("Login", "Account");
        }

patness
 
Posts: 10
Joined: Tue Jul 08, 2014 12:15 am

Tue Feb 17, 2015 10:45 pm

patness wrote:Ok, thank you. For anyone interested, here is a sample asp.net mvc 5 controller that can use the plugin and can respond with the pdf. Also, I noticed the plugin doesn't retain a background image so the code below inserts one. (My actual controller saves the file to blob storage, etc... this is just to help anyone else.)

Code: Select all
        public ActionResult ViewPdf(string path)
        {
            if (User.Identity.IsAuthenticated)
            {
                var url = path;
                byte[] bgImagePath = null;
                var backgroundImage = ...;  //This would be the path to the background Image

                if (!String.IsNullOrEmpty(backgroundImage))
                {
                    bgImagePath =  ...;  controller that gets the image bytes of the background image;
                }

                var subPath = "/Content/UserFiles/TMP/TMPDOC/";

                var exists = Directory.Exists(Server.MapPath(subPath));

                if (!exists)
                    Directory.CreateDirectory(Server.MapPath(subPath));

                var fullPath = subPath + "mypdf.pdf";
                fullPath = Server.MapPath(fullPath);

                HtmlConverter.Convert(url, fullPath, true, 100 * 1000, new SizeF(612, 792), new PdfMargins(0, 0));

                using (var ms = new MemoryStream())
                {
                    using (var fs = System.IO.File.OpenRead(fullPath))
                    {
                        fs.CopyTo(ms);
                    }
                    ms.Position = 0;

                    //no need to retain document in the file system
                    System.IO.File.Delete(fullPath);

                    //Create a pdf document.
                    var doc = new PdfDocument();
                    var thread = new Thread(() =>
                    {
                        doc.LoadFromStream(ms);
                    });
                    thread.SetApartmentState(ApartmentState.STA);
                    thread.Start();
                    thread.Join();

                if (!IsBgNull)
                {
                    var imgStr = new MemoryStream(bgImagePath);
                    var img = Image.FromStream(imgStr);
                    imgStr.Position = 0;
                    foreach (PdfPageBase page in doc.Pages)
                    {
                        page.BackgroundImage = img;
                    }
                    imgStr.Dispose();
                }

                    using (var ms2 = new MemoryStream())
                    {
                        doc.SaveToStream(ms2, FileFormat.PDF);
                        doc.Close();

                        Response.ContentType = "application/pdf";
                        Response.AddHeader("Content-Disposition", "attachment; filename=" + "mypdf.pdf");
                        ms2.Position = 0;
                        ms2.CopyTo(Response.OutputStream);

                    }
                }

                return new EmptyResult();
            }
            return RedirectToAction("Login", "Account");
        }

This method seems to work locally. On the server, it just spins eventually returning nothing but a blank screen. There is nothing in the event log. Will the plugin work on a webserver in this manner?

patness
 
Posts: 10
Joined: Tue Jul 08, 2014 12:15 am

Wed Feb 25, 2015 7:09 am

Dear patness,

Thanks for your inquiry.

We'll do some investigation about this problem and let you know ASAP.
Best Regards,
Burning
E-iceblue Support Team
User avatar

burning.liu
 
Posts: 406
Joined: Mon Aug 04, 2014 6:47 am

Thu Sep 29, 2016 2:37 pm

Has there been any progress on this issue? I'm having the same problem. However, my Web Server does have Internet Explorer installed on it. The version of IE is 11.0.9600.18163 running on Windows 2008 Server.

Our web service is running with an ApplicationPoolIdentity. Could that be the reason that it cannot access the installed version of IE? Or, is this IE version just too old?

As mentioned in prior posts, this works locally (running on Win10 Professional) but not on our deployed Production Server (Windows 2008).

Thanks,
Tom

rutzelt
 
Posts: 43
Joined: Mon Sep 22, 2014 4:59 pm

Fri Sep 30, 2016 8:51 am

Hello,

Thanks for your feedback. What's the issue you were running into? Which way you were using? If you use the plugin way, please make the IIS application pool to Enable 32-bit applications and try it again.

Sincerely,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Fri Sep 30, 2016 1:37 pm

I would prefer not to use the plugin way as I've already noticed problems with images.

I'm having the same issue as described in this thread. However, IE is installed on our server where the application is deployed. The version is 11.0.9600.18163. According to this thread, it implies that it occurs when IE is NOT installed. As ours is installed, is there something we need to do to have our service use the IE engine? Or, is there a specific problem with the version of the OS or version of IE that we are using?

Thank you.

rutzelt
 
Posts: 43
Joined: Mon Sep 22, 2014 4:59 pm

Mon Oct 03, 2016 6:52 am

Dear rutzelt,

Thanks for your information and sorry for late reply as weekend.
Did you mean you encounter the issue "could not load file or assembly 'Microsoft.mshtml... ", did you refenrence the Microsoft.mshtml.dll ? And we will investigate it and update to you. Sorry for inconvenience caused.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Mon Oct 03, 2016 9:42 pm

If we add the reference to the DLL and deploy it to the "bin" directory of the production server, we get a new exception:

Parameter name: url</Message><StackTrace> at Spire.Pdf.HtmlConverter.HtmlConverter.0(String A_0, ImageType A_1, Int32 A_2)
at Spire.Pdf.HtmlConverter.HtmlConverter.Convert(String url, ImageType type, Int32 width, Int32 height, AspectRatio aspectRatio)
at Spire.Pdf.PdfSection.0(String A_0, Boolean A_1, Boolean A_2, Boolean A_3, PdfHtmlLayoutFormat A_4)
at Spire.Pdf.PdfDocument.LoadFromHTML(String htmlSourceCode, Boolean autoDetectPageBreak, PdfPageSettings setting, PdfHtmlLayoutFormat layoutFormat)

This error does not occur on our local development server. Is there another reference we should try adding?

Thanks,
Tom

rutzelt
 
Posts: 43
Joined: Mon Sep 22, 2014 4:59 pm

Tue Oct 04, 2016 11:50 am

Hello,

Thanks for your response. What is your system? It would be helpful if you could provide us the url and the codes you were using.

Thanks in advance,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Tue Oct 04, 2016 10:57 pm

From prior post:
The version of IE is 11.0.9600.18163 running on Windows 2008 Server.

I got it to work. I did need to put the MSHTML.dll in the bin directory though.

Thanks.
-Tom

rutzelt
 
Posts: 43
Joined: Mon Sep 22, 2014 4:59 pm

Wed Oct 05, 2016 6:22 am

Hello,

Thanks for your feedback. Glad to hear that.

Sincerely,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Return to Spire.PDF