html to pdf

Technical support for Spire.PDF

Moderator: iceblue support

html to pdf

Postby spiresupport » Thu Aug 04, 2011 3:59 am

Hi ,
We are populating the page using the parameter from the previous page with session variable.
We need to save the pdf to database. How can we convert this page's html to pdf or Stream.
spiresupport
 
Posts: 8
Joined: Tue Jul 19, 2011 5:55 am

Re: html to pdf

Postby Justin Weng » Thu Aug 04, 2011 5:44 am

Dear spiresupport,
Thanks for your inquiry.
You can use Spire.Doc to do so.
Here is a sample code:
Code: Select all
            Document doc = new Document();
            doc.LoadFromFile("test.html", FileFormat.Html);
            //doc.SaveToFile("result.pdf", FileFormat.PDF);
            using (MemoryStream stream = new MemoryStream())
            {
                doc.SaveToStream(stream, FileFormat.PDF);
                //Save to database....
            }
Justin
Technical Support / Developer,
e-iceblue Support Team
User avatar
Justin Weng
 
Posts: 110
Joined: Mon Mar 28, 2011 5:54 am

Re: html to pdf

Postby spiresupport » Thu Aug 04, 2011 7:31 am

Dear Justin,

In my case, I can't use the doc.LoadFromFile() since I don't have the physical file.
I am using the aspx page and populate the page's content from the database with the parameter getting from the session variable that passed from the previous page.
Let say I have "Registration.aspx". When the registration is success, I am setting the "RegistrationID" in the session variable, and redirect to " RegistrationSuccess.aspx" page.
The content of "RegistrationSuccess.aspx" will be getting from the database with the parameter "RegistrationID".
In that case, how do I convert the ReigstrationSuccess page's html to pdf?

Thanks.
spiresupport
 
Posts: 8
Joined: Tue Jul 19, 2011 5:55 am

Re: html to pdf

Postby Justin Weng » Thu Aug 04, 2011 7:55 am

Dear spiresupport,
Thanks for your inquiry.
Maybe you can use this method.
Code: Select all
public void LoadFromHTML(string Url, bool enableJavaScript, bool enableHyperlinks, bool autoDetectPageBreak);

When redirect to " RegistrationSuccess.aspx" page, use this method.
Hope help you.
Justin
Technical Support / Developer,
e-iceblue Support Team
User avatar
Justin Weng
 
Posts: 110
Joined: Mon Mar 28, 2011 5:54 am

Re: html to pdf

Postby spiresupport » Thu Aug 04, 2011 8:33 am

Hi Justin,
The followning exception thrown while calling doc.LoadFromHTML()
ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.

Any Idea?
spiresupport
 
Posts: 8
Joined: Tue Jul 19, 2011 5:55 am

Re: html to pdf

Postby spiresupport » Thu Aug 04, 2011 9:01 am

Thanks Justin. Working perfectly.
spiresupport
 
Posts: 8
Joined: Tue Jul 19, 2011 5:55 am

Re: html to pdf

Postby ebarton » Fri Dec 09, 2011 3:24 am

I am getting the same error "ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment."

How do I solve it?

I have created HTMLTable dynamically in codebehind and want to convert it to PDF. How do i get it???

Thanks
ebarton
 
Posts: 7
Joined: Thu Dec 08, 2011 9:13 am

Re: html to pdf

Postby Tina.Lin » Mon Dec 12, 2011 3:52 am

Hello ebarton,

Thank you for your inquiry.

Most program need to run in a single thread. Try to create a new thread may resolve the problem.

Please try the following code:
Code: Select all
using System;
using System.Threading;

using Spire.Pdf;

namespace SingleThreadTest
{

    public partial class Test : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {   
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //new a thread
            ThreadStart threadStart = HTMLToPDF;
            Thread thread = new Thread(threadStart);
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }
        void HTMLToPDF()
        {
            PdfDocument doc = new PdfDocument();

            String url = "http://www.e-iceblue.com/";
            doc.LoadFromHTML(url, false, true, true);

            //Save pdf file.
            string path=Server.MapPath(@".\Test.pdf");
            doc.SaveToFile(path);
            doc.Close();

            //Launching the Pdf file.
           
        }
    }
}


If you still have any other questions, please don't hesitate to contact us.

Have a nice day.
Tina
Technical Support/Developer,
e-iceblue Support Team
User avatar
Tina.Lin
 
Posts: 152
Joined: Tue Sep 13, 2011 5:37 am

Re: html to pdf

Postby ebarton » Mon Dec 19, 2011 2:08 am

Thanks Tina,

The solution worked like a charm.......

But one more problem: it produce pdf file on local server but when I upload the site on live server with all necessary files into Bin folder, It is not popping up pdf file or neither shows any error message.

Am I missing something????

Thank you once again for your support.
ebarton
 
Posts: 7
Joined: Thu Dec 08, 2011 9:13 am

Re: html to pdf

Postby ebarton » Tue Dec 20, 2011 7:59 am

After couple of testing sample just find out that it's not calling method at all.

On localhost it shows that it has created new thread and executed HTMLToPDF method while debugging. But on live server it seems like it's not calling method at all. I am not sure if it creates thread or not. What should I do?????

Thanks
ebarton
 
Posts: 7
Joined: Thu Dec 08, 2011 9:13 am

Re: html to pdf

Postby Tina.Lin » Wed Dec 21, 2011 10:04 am

Hello ebarton,

Sorry for late reply.

You can try to save the pdf file as stream and then write the stream to "Response".

Have a nice day.
Tina
Technical Support/Developer,
e-iceblue Support Team
User avatar
Tina.Lin
 
Posts: 152
Joined: Tue Sep 13, 2011 5:37 am

Re: html to pdf

Postby ebarton » Thu Dec 22, 2011 4:25 am

Thanks for your reply but I have found that the HTMLTOPDF method is not executed. It seems like the new thread is not running at all.

On localhost it shows that it has created new thread and executed HTMLToPDF method while debugging. But on live server it seems like it's not calling method at all. I am not sure if it creates thread or not. What should I do?????

Thanks
ebarton
 
Posts: 7
Joined: Thu Dec 08, 2011 9:13 am

Re: html to pdf

Postby Tina.Lin » Fri Dec 23, 2011 8:24 am

Hello ebarton,

Sorry for late reply and thank you for your patience.

We have researched on this issue. Caused the exception "ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment." is that the html file you want to load is plugged in ActiveX control. We made you a workaround to avoid the issue. Please try the following code:

Code: Select all
using System;
using System.Threading;
using System.IO;

using Spire.Pdf;

namespace SingleThreadTest
{
    public partial class Test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            //new a thread     
            Thread thread = new Thread(HTMLToPDF);
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();

            PdfDocument pdfDoc = new PdfDocument(Server.MapPath(@".\Test.pdf"));
            //save pdf file to browser
            pdfDoc.SaveToHttpResponse("Test.pdf", Response, HttpReadType.Save);
        }

        void HTMLToPDF()
        {
            PdfDocument doc = new PdfDocument();
            String url = "http://www.e-iceblue.com/";
            doc.LoadFromHTML(url, false, true, true);

            doc.SaveToFile(Server.MapPath(@".\Test.pdf"));
            doc.Close();
        }
    }
}


If you still have any other problems, please don't hesitate to contact us.

Marry Chrismas in advance.
Have a nice day.
Tina
Technical Support/Developer,
e-iceblue Support Team
User avatar
Tina.Lin
 
Posts: 152
Joined: Tue Sep 13, 2011 5:37 am

Re: html to pdf

Postby ebarton » Wed Jan 04, 2012 4:13 am

Hi

Happy New Year!

I still have problem. The code you have given is working well on localhost but on server it's not generating any PDF File.

I have tried to attach html file as attachment but it's not allowing to attach HTML File or txt file or docx file. It is a simple html page no ActiveX control. You can check that HTML file here: http://www.ctilogistics.com/test/Report_6041.html

On a click of Generate button my code is:
'new a thread
Dim Thread1 As New Thread(AddressOf HTMLTOPDF)
Thread1.SetApartmentState(ApartmentState.STA)
Thread1.Start()


In HTMLTOPDF:

FileName = Fname.Value.ToString

If FileName.ToString.Trim <> "" Then
Dim strFilePath = "http://203.153.233.40/foxpod/PDF/" & FileName & ".html"

Dim pdfDoc As New PdfDocument
pdfDoc.LoadFromHTML(strFilePath, False, True, True)

Dim path As String = ""
path = Server.MapPath("~/PDF/") & FileName & ".pdf"
pdfDoc.SaveToFile(path)
pdfDoc.Close()

End If


Why it is not generating PDF file??? What am I doing wrong??? It is generating PDF file in localhost but not on the server. Is that the licence I have purchased is only allows to use this for one site and is it considering localhost as a single site and when I upload it to live server it understands that this is another site and stops working..???

Now it is getting frustrating.

Can you help me please ASAP...................
ebarton
 
Posts: 7
Joined: Thu Dec 08, 2011 9:13 am

Re: html to pdf

Postby Tina.Lin » Wed Jan 04, 2012 9:14 am

Hello ebarton,

So sorry for the late response.

At this momment we can't reproduce the issue you mentioned. Do you get any exceptions when you try to write data into the pdf file on the server? Would you please check whether the current account you used has access to the file on server?
So sorry for the inconvenience caused by us. Once you have have any informations, would you please inform us better to reproduce the issue?

Thanks in advance.
Have a nice day.
Tina
Technical Support/Developer,
e-iceblue Support Team
User avatar
Tina.Lin
 
Posts: 152
Joined: Tue Sep 13, 2011 5:37 am

Next

Return to Spire.PDF

Who is online

Users browsing this forum: No registered users and 0 guests