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.

Mon Apr 28, 2014 3:15 am

I'm trying to generate pdfs based off html pages generated by ASP.NET MVC. The pages also contain javascript. The javascript needs to run before the pdf is created to display the correct values.

The pdf is getting generated ok but the javascript has not run even though I've set the enableJavascript parameter of the LoadFromHTML method to true. My code looks a bit like this:

Code: Select all
                PdfDocument doc = new PdfDocument();
                var url = "http://localhost:90/index/1234";

                // create the pdf
                var thread = new Thread(() => doc.LoadFromHTML(url, true, true, false));
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
                thread.Join();

                // return the pdf result
                doc.SaveToHttpResponse("Results.pdf", httpResp, HttpReadType.Open);
                doc.Close();
                httpResp.Close();



The page "localhost:90/index/1234" which we are trying to convert to pdf has a reference to AngularJS:
Code: Select all
<script type="text/javascript" src="/content/lib/angular.min.js"></script>

and some simple javascript that to populate some data.

The page works fine in the browser but the javascript does not run when converting to PDF.

Any suggestions?

Thanks
Craig

craigaustin
 
Posts: 2
Joined: Mon Apr 28, 2014 2:50 am

Mon Apr 28, 2014 7:03 am

Hello,

Thanks for your inquiry.

Please try the method to Convert HTML to PDF with New Plugin, which link is
http://www.e-iceblue.com/Knowledgebase/ ... o-PDF.html

If the issue still exists, could you please provide us a simple project to produce the issue? It would be helpful to work out the solution for you ASAP.

Sincerely,
Gary
E-iceblue support team
User avatar

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

Tue Apr 29, 2014 12:03 am

Thanks Gary for the quick response.

The pdf file is created correctly now after the javascript has executed.

With this approach it is creating a physical file on the server. Is there a way instead that I can write the file directly to the http response so I don't have to save the file on the server?

Thanks
Craig

craigaustin
 
Posts: 2
Joined: Mon Apr 28, 2014 2:50 am

Tue Apr 29, 2014 1:47 am

Hello,

Sorry that with the method there is no way to write it to the response directly, you have to save the file on the server.

If there are any questions, welcome to get it back to us.

Sincerely,
Gary
E-iceblue support team
User avatar

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

Tue Apr 29, 2014 7:22 am

Hello.
I have the same problem.
I have ASP.NET MVC app in Azure.
And i can't save file on hard drive.
So, there is no way to convert my Html (css+js+angular) on fly directly to memory stream. Right?

dskibin
 
Posts: 2
Joined: Mon Apr 28, 2014 6:31 pm

Tue Apr 29, 2014 8:39 am

Hello,

Yes, with this method there is no way to write it to stream directly, you can create a temporary file and convert it to stream, then delete it.

If there are any questions, welcome to get it back to us.

Sincerely,
Gary
E-iceblue support team
User avatar

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

Tue Apr 29, 2014 8:53 am

I do not have access to hard drive on Azure :(

dskibin
 
Posts: 2
Joined: Mon Apr 28, 2014 6:31 pm

Tue Apr 29, 2014 9:19 am

Sorry for inconvenience. The feature will be added in the future, but I have just communicated with our Dev team that it would be so difficult that it is not completed in a short time.

If there are any questions, welcome to get it back to us.

Thanks,
Gary
E-iceblue support team
User avatar

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

Wed May 15, 2019 12:42 pm

I am trying to Convert HTML to Pdf using the below code. But is not giving me the result because the thread is taking too much of time to load(say 2 minutes) and it is throwing an error. I tried using Task Parallel Library, but it is giving me error "The Active x control support STA". Can you post a solution for this problem.

public void ExportToPdf()
{
Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument();
Spire.Pdf.PdfPageSettings setting = new Spire.Pdf.PdfPageSettings();
setting.Margins = new Spire.Pdf.Graphics.PdfMargins(20);
PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat();
htmlLayoutFormat.IsWaiting = true;
String url = Request.Url.GetLeftPart(UriPartial.Authority) + "/Settings/psa_district_wise-report_Pdf.aspx";

/*var scheduler = TaskScheduler.FromCurrentSynchronizationContext();
Task[] tasks = new Task[1]
{
Task.Factory.StartNew(() => doc.LoadFromHTML(url,false,false,false,setting,htmlLayoutFormat))
};

Task.WaitAll(tasks);*/

System.Threading.Thread thread = new System.Threading.Thread(() =>
{ doc.LoadFromHTML(url, false, false, false, setting,htmlLayoutFormat); });

thread.SetApartmentState(System.Threading.ApartmentState.STA);
thread.Start();
thread.Join();

//Save pdf file.
doc.SaveToFile(Server.MapPath("~/Documents/psa_district_wise-report_Pdf.pdf"));
doc.Close();

//System.Diagnostics.Process.Start(Server.MapPath("~/Documents/psa_district_wise-report_Pdf.pdf"));
}

lakshmi.adapa05@gmail.com
 
Posts: 1
Joined: Fri Apr 12, 2019 5:50 am

Thu May 16, 2019 9:03 am

Hi,

Thanks for your inquiry.
After an initial test with our latest Spire.PDF Pack(Hot Fix) Version:5.5.1. I didn't encounter any issue on my side. My sample aspx can be converted well. You mentioned "taking too much of time to load(say 2 minutes) and it is throwing an error", what is the specific exception thrown? Besides, our Spire.PDF provides another method to convert HTML to PDF. Please refer to the tutorial: How to convert HTML to PDF with New Plugin. I would suggest that you download the latest Spire.PDF and have a try with the Plugin method. If the issue still happens after trying, please provide your ASPX file to help us further look into your issue. You could send it to us via email (support@e-iceblue.com). Thanks in advance.

Sincerely,
Nancy
E-iceblue support team
User avatar

nancy.yang
 
Posts: 184
Joined: Wed Apr 03, 2019 2:33 am

Mon May 20, 2019 9:56 am

Hi,

How is your issue now? Could you please give us some feedback at your convenience? Thanks in advance.

Sincerely,
Nancy
E-iceblue support team
User avatar

nancy.yang
 
Posts: 184
Joined: Wed Apr 03, 2019 2:33 am

Return to Spire.PDF