We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Exception using Aspose.PDF to convert HTML to PDF and Save to HttpResponse

I am trying to use Aspose.PDF to convert an HTML document to PDF and then return it to the browser using an HTTP Response. The relevant code is:

Aspose.Pdf.HtmlLoadOptions plo = new Aspose.Pdf.HtmlLoadOptions();
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(filePathSource, plo);
Aspose.Pdf.PdfSaveOptions pso = new Aspose.Pdf.PdfSaveOptions();
pdfDocument.Save(response, fileName, Aspose.Pdf.ContentDisposition.Attachment, pso);

The Save triggers a System.Threading.ThreadAboortException. The exception contents are null.

Note: I am using similar code to convert Word to PDF and return as a request response. The following code works:

Aspose.Words.Document wordDocument = new Aspose.Words.Document(filePathSource);
Aspose.Words.Saving.PdfSaveOptions saveOptions = new Aspose.Words.Saving.PdfSaveOptions();
wordDocument.Save(response, fileName, Aspose.Words.ContentDisposition.Attachment, saveOptions);

Have a missed something in setting up the SaveOptions for the HTML to PDF to HTTPResponse code?

@jburnett

Could you kindly share a sample web application in .zip format for our reference? We will test the scenario in our environment and address it accordingly. Also, please make sure to test the case with 22.7 version before sharing the requested information.

I can confirm that I am using the latest versions of all Aspose libraries.
The system I am working on is an aspx based website that is quite complex, so it is too difficult to prepare a self-contained working sample of the issue. However, I have prepared a sub-set of the code that is relevant to the issue. It has been zipped to here:

Test_Viewer.aspx.zip (2.5 KB)

The code is intended to accept a file name in an HTTP request and return the PDF version of the requested file. It is working for some file types, but not others.
The issues occurs in Render_to_HTTP. The calls to:
wordDocument.Save(response, fileName, Aspose.Words.ContentDisposition.Attachment, saveOptions)
and
excelWorkbook.Save(response, fileName, Aspose.Cells.ContentDisposition.Attachment, pso);
are working as expected.
The calls to:
pdfDocument.Save(response, fileName, Aspose.Pdf.ContentDisposition.Attachment, pso);
are not working and generate an exception.
Also, there is no equivalent Save method for Aspose.Slides, so I am trying to use the stream option to response.OutputStream, but that does not work.
Any assistance would be appreciated!

@jburnett

We will surely try to replicate the issue in our environment. In case you cannot share a sample application, can you please share the .NET Framework Version in which you are working?

I am using .NET Framework 4.8 with target platform of x64.

@jburnett

We are checking it and will get back to you shortly.

@jburnett

We used the following simple code snippet in a sample web application with the same configuration and were unable to notice any issue. Can you please try to create an empty webform application and try the below code to see whether it is working or not? In case you still face any issues, please share the same application with us so that we can further proceed to assist you accordingly.

// C# Code
var pdfDocument = new Aspose.Pdf.Document();
// make some manipulation, i.g add a new empty page
pdfDocument.Pages.Add();
pdfDocument.Save(Response, "Filename.pdf", Aspose.Pdf.ContentDisposition.Attachment, new Aspose.Pdf.PdfSaveOptions());

PS: We tested using 22.7 version of the API.

Thank you for investigating the issue. The snippet you have provided is essentially the same as the code I have implemented, so I believe we are on the right track.
Further investigation appears to suggest the actual pdfDocument.Save is working: the response object is being returned with the PDF version of the file as an attachment.
However, my code is still triggering an exception. The exception is ‘Thread was being aborted’. Additional research suggests this may be caused by a Response.End being done as part of the pdfDocument.Save. Refer to https://docs.microsoft.com/en-US/troubleshoot/developer/webapps/aspnet/development/threadabortexception-occurs-you-use-response-end for more details.
Can you confirm if:

  1. A Response.End is being used within pdfDocument.Save
  2. The pdfDocument.Save is similar to the wordDocument.Save with the same parameters (which does not trigger an exception in my code)

@jburnett

We need to further investigate this case. Could you please share the complete Exception Message and Error Stack Trace for our reference as well?

theException = System.Threading.ThreadAbortException: Thread was being aborted.
at System.Threading.Thread.AbortInternal()
at System.Threading.Thread.Abort(Object stateInfo)
at System.Web.HttpResponse.AbortCurrentThread()
at Aspose.Pdf.Document.#=zTEJEq40tN4NA(HttpResponse #=zEOYcTyo=, String #=zA$bSXa0Nfxwm, ContentDisposition #=zA8dOY$4gZqpVA7qGLw==, SaveOptions #=z48c_1lM=)
at Aspose.Pdf.Document.Save(HttpResponse response, String outputFileName, ContentDisposition disposition, SaveOptions options)
at Zenesi.Class.FileDistillation.Render_Blob_to_HTTP(String fileNameIn, Stream blobFile, HttpResponse& response) in C:\Source\Workspaces\Zenesi_WPF\Projects\Class\Zenesi.Class\FileDistillation.cs:line 3247

@jburnett

An investigation ticket as PDFNET-52264 has been logged in our issue tracking system for further analysis. We will look into its details and keep you posted with the status of its rectification. Please be patient and spare us some time.

We are sorry for the inconvenience.