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.

Wed Jul 08, 2020 12:54 am

I have a runtime error, Please help! Here is my code

Imports Spire.Pdf
Imports Spire.Pdf.HtmlConverter

Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click

Dim doc As New PdfDocument()

Dim pgSt As New PdfPageSettings()
pgSt.Size = PdfPageSize.A4
Dim htmlLayoutFormat As New PdfHtmlLayoutFormat()
htmlLayoutFormat.IsWaiting = False
-----> doc.LoadFromHTML("get-onlinequotes.com/calculators/Review.aspx", True, True, True) <------- ERROR HERE




Error is
Server Error in '/' Application.
ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.

ianwinbolt
 
Posts: 4
Joined: Mon Jul 06, 2020 7:03 am

Wed Jul 08, 2020 5:50 am

Hello,

Thanks for your inquiry.
It is only used in the single thread, so you need to achieve it with single thread. Please refer to the code below.
Code: Select all
    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim threadStart As ThreadStart = AddressOf HTMLToPDF

        Dim thread As New Thread(threadStart)

        thread.SetApartmentState(ApartmentState.STA)
        thread.Start()
    End Sub

    Private Sub HTMLToPDF()

        Dim doc As New PdfDocument()

        Dim pgSt As New PdfPageSettings()
        pgSt.Size = PdfPageSize.A4
        Dim htmlLayoutFormat As New PdfHtmlLayoutFormat()
        htmlLayoutFormat.IsWaiting = True
        doc.LoadFromHTML("get-onlinequotes.com/calculators/Review.aspx", True, True, True)
        doc.SaveToFile("Result.pdf", FileFormat.PDF)
        doc.Close()
    End Sub


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Wed Jul 08, 2020 7:46 am

Thanks Rachel,

Unfortunately there is still an issue,

The line : doc.LoadFromHTML("get-onlinequotes.com/calculators/Review.aspx", True, True, True)
Doesn't crash but seems to halt the code and no pdf is created when saved.

if I exclude this line, an empty pdf is created.

ianwinbolt
 
Posts: 4
Joined: Mon Jul 06, 2020 7:03 am

Wed Jul 08, 2020 8:52 am

Hello,

Thanks for your inquiry.
Please make sure that your computer can access the webpage normally, and you can also try another URL. And to help us further investigate it, please provide your environment information, such as OS information (E.g. Windows 7, 64 bit) and region setting (E.g. China, Chinese). Thanks in advance.
Besides, there is another plugin method to convert HTML to PDF, which performs better than the normal method. You can also try it.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Mon Aug 03, 2020 5:16 am

Hi,
I am also having same issue with thread.
Thread is not working in AZURE environment. If I use convertfromHtml method getting plugin not found issue.
Please help me to resolve it.
While using Thread I am getting below issue

502 - Web server received an invalid response while acting as a gateway or proxy server.
There is a problem with the page you are looking for, and it cannot be displayed. When the Web server (while acting as a gateway or proxy) contacted the upstream content server, it received an invalid response from the content server.

manojraog
 
Posts: 11
Joined: Mon Aug 03, 2020 4:51 am

Mon Aug 03, 2020 6:31 am

Hi Manoj,

Thanks for your post.
On Azure, we suggest you use plugin method to convert html to PDF. Meanwhile, when using the x64 plugin, please make sure you have changed the platform of the general settings to 64 Bit, you can refer to this post (https://www.e-iceblue.com/forum/the-conversion-provider-cann-t-be-found-t9410.html).

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Mon Aug 03, 2020 10:23 am

Hello, Thanks for your reply..

For the plugin method we are not able to get CSS what we are expecting. Also I got below issue while using plugin method.
The conversion provider cann't be found. Please make sure: 1. the folder "plugins" under the same folder of Spire.Pdf.dll 2. exists "plugins\HTMLConverter.dll" and other "plugins\*.dll" at Spire.Pdf.HtmlConverter.Qt.HtmlConverter.init(String url, Boolean enableJavaScript, Int32 timeout, SizeF pageSize, PdfMargins margins) at Spire.Pdf.HtmlConverter.Qt.HtmlConverter.Convert(String url, String fileName, Boolean enableJavaScript, Int32 timeout, SizeF pageSize, PdfMargins margins) at Spire.Pdf.HtmlConverter.Qt.HtmlConverter.Convert(String url, String fileName, Boolean enableJavaScript, Int32 timeout, SizeF pageSize, PdfMargins margins, LoadHtmlType urlHtml)

How to configure plugin path in our code if we use PLUGIN Method.

Please provide solution for LOADFromHTML method
we are using spire.pdf version: 5.6.3.18046
spire.doc version: 7.6.2.18046

manojraog
 
Posts: 11
Joined: Mon Aug 03, 2020 4:51 am

Mon Aug 03, 2020 10:40 am

Hello Manoj,

Thanks for your response.
In fact, the plugin method performs better than the normal method. And there are indeed some problems when using the normal method to convert html to PDF on Azure, sorry there is no good solution to solve it at present.

Regarding the CSS style issue you mentioned, please provide your URL or html file for our further investigation.

As for error "The conversion provider cann't be found" you got, are you using the x64 plugin? If so, as I mentioned earlier, please make sure you have changed the platform of the general settings to 64 Bit. And make sure the folder "plugins" is under the same folder of Spire.Pdf.dll in your Azure application, or you could directly specify the plugin path as shown below.
Code: Select all
Spire.Pdf.HtmlConverter.Qt.HtmlConverter.PluginPath = "path";


If this does not solve your issue, please provide your project to help us further look into it. You could upload it to the DropBox or OneDrive and then share the download link with us. Thanks in advance.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Mon Aug 03, 2020 4:03 pm

Hi Rachel,
Thanks for your support
As you mentioned plugins and spire.pdf.dll are in same folder.
could you please tell me how to configure plugin path from code level with an example.
Also please let us know is there any specific way to add external CSS to html raw code.
we are using in the following way

string cssStyles = System.IO.File.ReadAllText(Server.MapPath(@"/StaticContent/CSS/MVC/xyz/abc.css"));
string htmlCode = "<html><head><style type='text/css'>" + cssStyles + "</style></head><body>" + htmlText + "</body></html>";

manojraog
 
Posts: 11
Joined: Mon Aug 03, 2020 4:51 am

Tue Aug 04, 2020 7:06 am

Hello Manoj,

Thanks for your response.
Regarding configuring plugin path and adding external CSS, I made a demo for you, please run it directly on your side. Feel free to contact us if there is any question.
http://www.e-iceblue.com/downloads/demo/22400Demo.zip

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.PDF

cron