HI ALL,
I need to convert the HTML String to generate a PDF file , and open/download that same PDF file on the browser.
I have tried following code , but it didnt work well.
public void CreatePDF1()
{
string MyDir = @“c:\temp\Test11”;
string fileName = MyDir + " EC.htm";
// Open the stream.
Stream stream = File.OpenRead(fileName);
LoadOptions loadOptions = new LoadOptions();
loadOptions.BaseUri = MyDir;
stream.Close();
string str2 = “PDF_” + DateTime.Now.ToString().Replace(’:’, ‘_’).Replace(’/’, ‘-’) + “.pdf”;
Aspose.Pdf.Document doc2 = new Aspose.Pdf.Document(fileName);
doc2.Save(stream, Aspose.Pdf.SaveFormat.Pdf);
stream.Position = 0;
}
I get the followign error:
<span style=“font-size:11.0pt;font-family:“Calibri”,“sans-serif”;
mso-fareast-font-family:Calibri;mso-fareast-theme-font:minor-latin;mso-bidi-font-family:
“Times New Roman”;mso-ansi-language:EN-US;mso-fareast-language:EN-US;
mso-bidi-language:AR-SA”>Startxref not found
Please Help
Hi Vishal,
Thanks for your inquiry. Please use the DOM approach to convert HTML to PDF, save the output document in a stream, and send the document to the browser. Please check the following code snippet for the purpose. Hopefully, it will help you accomplish the task.
// Specify the base path/url for the html file which serves as images database
String basePath = "C:/temp/";
MemoryStream stream = new MemoryStream();
HtmlLoadOptions htmloptions = new HtmlLoadOptions(basePath);
// use the new conversion engine
htmloptions.UseNewConversionEngine = true;
// load HTML file
Document doc = new Document("input.html", htmloptions);
// Save HTML file
doc.Save(stream);
Response.Clear();
// Specify the document type.
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "inline; filename=ConvertedDoc.pdf");
byte[] bytes = stream.GetBuffer();
Response.BinaryWrite(bytes);
// System.IO.File.Delete(SaveLocation);
Response.Flush();
Response.Close();
Response.End();
Please feel free to contact us for any further assistance.
Best Regards,
Hi ,
Thanks for your reply…
But I dont want to save the file in any location in as mentioned below.
String basePath = “C:/temp/”;
I just want to return the MemoryStream object.
Regards,
Vishal
Hi Vishal,
Thanks for your feedback. Please note basePath is not used to save but reading the input HTML file. Please note HtmlLoadOptions() constructor with path string parameter is used if your input HTML file has some external resources etc. images or CSS files etc.
Moreover, if you only want to use html string to create a PDF file. Then you may use HtmlFragment object for the purpose. Please check following documentation link for the purpose.
Please feel free to contact us for any further assistance.
Best Regards,