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

Free Support Forum - aspose.com

Exception when convert html to pdf

Hi there

I’m using Aspose PDF for .net 18.12.

i try to convert html to pdf but i get only exception (illegal characters in the path).

below is my code.


// first try
string src = “d:\html.html”;
string dst = “d:\html.html.pdf”;

try
{
FileStream fs = new FileStream(src, FileMode.Open, FileAccess.Read);
MemoryStream stream = new MemoryStream();
fs.CopyTo(stream);

HtmlLoadOptions options = new HtmlLoadOptions(Path.GetDirectoryName(src));	
Document pdfDocument = new Document(stream, options); // !!!!! exception here !!!!!
pdfDocument.Save(dst);

}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}


// second try

string src = “d:\html.html”;
string dst = “d:\html.html.pdf”;
try
{
HtmlLoadOptions options = new HtmlLoadOptions(Path.GetDirectoryName(src));
Document pdfDocument = new Document(src, options); // !!! exception here !!!
pdfDocument.Save(dst);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}


// third try

string dst = “d:\html.html.pdf”;

ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.

// Create a request for the URL.
WebRequest request = WebRequest.Create(“https://En.wikipedia.org/wiki/Main_Page”);
// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
// Time out in miliseconds before the request times out
// Request.Timeout = 100;

// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();

MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(responseFromServer));
HtmlLoadOptions options = new HtmlLoadOptions(“https://En.wikipedia.org/wiki/”);

// Load HTML file
Document pdfDocument = new Document(stream, options); // !!! exception here !!!

options.PageInfo.IsLandscape = true;

// Save output as PDF format
pdfDocument.Save(dst);

@hj.alio

We have tested the scenario in our environment using Aspose.PDF for .NET 20.9 and were unable to replicate the exception that you have shared. Would you kindly share your sample HTML file in .zip format with us so that we can test the scenario in our environment and address it accordingly.

I have tested it using Aspose.PDF for .NET 20.9 and i got the same exception. I attached my sample HTML file in .zip format.

illegalCharactersInThePath.zip (19.8 KB)

@hj.alio

We tested the scenario using following code snippet and Aspose.PDF for .NET 20.9 in our environment. We were unable to notice the exception:

HtmlLoadOptions options = new HtmlLoadOptions();
options.IsRenderToSinglePage = true; // if we remove this line, the formatting is more bad in output
//options.IsEmbedFonts = true;
options.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);
Document doc = new Document(dataDir + "illegalCharactersInThePath.html", options);
doc.Save(dataDir + "output.pdf");

output.pdf (208.9 KB)

However, we noticed that the formatting in output PDF was disturbed. For the sake of correction, we have logged an issue as PDFNET-48869 in our issue tracking system. We will further look into its details and keep you informed about the status of its rectification. Please be patient and spare us some time.

Regarding the exception that you are facing, would you kindly share a sample console application for our reference which is able to reproduce it in our environment? We will again test the scenario and address it accordingly.

Thanks to your reply!

I have tested my code&binary in another PC.
And the result is same to your reply.

currenttly, the exception is exist only on my PC.
(Windows 10 Pro ver 1803 - build 17134.1246)

if i have the same problem on the other PC,
what information do i have to gather for reporting to you?

@hj.alio

It seems environment specific issue. It is possible that file path which you are supplying in the code may contain some illegal characters or it can be longer. You may please share a sample console application with hardcoded paths in order to replicate the issue in our environment so that we can investigate it further.