Free Support Forum - aspose.com

Convert HTML String to PDF using DOM Approach in Aspose.PDF for .NET

Hi team,
I have included the Aspose.PDF dll version 17.7.0.0 from Nuget packages in Visual Studio.
I am trying to convert a div HTML into PDF version with the below mention code in Aspose.

String html = “this is a link to a web site like <a href=“http://www.aspose.com/”>this.”;
//instantiate HtmlLoadOptions object and set desrired properties.
HtmlLoadOptions htmlLoadOptions = new HtmlLoadOptions();
htmlLoadOptions.PageInfo.Margin.Bottom = 10;
htmlLoadOptions.PageInfo.Margin.Top = 20;
//Load HTML string
Document doc = new Document(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html)), htmlLoadOptions);
//Save PDF file
doc.Save(“output.pdf”);
But the page is giving error file not found.No BreakPoint is also coming in Visual Studio from the htmlLoadOptions.But if I remove the above mention code then breakpoint is also coming.

I saw another example code
string dataDir = RunExamples.GetDataDir_AsposePdfGenerator_Conversion();

// Instantiate an object PDF class
Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();

// Add the section to PDF document sections collection
Aspose.Pdf.Generator.Section section = pdf.Sections.Add();

// Read the contents of HTML file into StreamReader object
StreamReader r = File.OpenText(dataDir + “resultant.html”);

// Create text paragraphs containing HTML text
Aspose.Pdf.Generator.Text text2 = new Aspose.Pdf.Generator.Text(section, r.ReadToEnd());

// Enable the property to display HTML contents within their own formatting
text2.IsHtmlTagSupported = true;

text2.IfHtmlTagSupportedOverwriteHtmlFontNames = true;
text2.IfHtmlTagSupportedOverwriteHtmlFontSizes = true;

// Add the text paragraphs containing HTML text to the section
section.Paragraphs.Add(text2);

// Specify the URL which serves as images database
pdf.HtmlInfo.ImgUrl = dataDir;

// Following properties are added from Aspose.Pdf for .NET 8.4.0
pdf.HtmlInfo.BadHtmlHandlingStrategy = BadHtmlHandlingStrategy.TreatAsPlainText;
pdf.HtmlInfo.ShowUnknownHtmlTagsAsText = true;

// Save the Pdf document
pdf.Save(dataDir + “HTML2pdf_out.pdf”);

But my version of dll even missing Aspose.Pdf.Generator namespace.

Kindly help if the problem is with the dll or I am missing something.

Regards,
Bidisha

@bidisha.c1

Thanks for contacting support.

Please note that Aspose.Pdf.Generator approach has been deprecated in new version(s) of Aspose.Pdf for .NET, you can now generate as well as manipulate existing PDF document(s) while using Aspose.Pdf DOM (Document Object Model) approach which is more enhanced and recommended approach as well.

In old legacy Aspose.Pdf.Generator model, there was a property called IsHtmlTagSupported which makes it possible to add HTML tags/contents into PDF files. Whereas in DOM, HtmlFragment instance can be used to specify the HTML contents which should be placed inside the PDF file. Similar to TextFragment, HtmlFragment is a paragraph level object and can be added to the Page paragraphs collection.

Please check following code snippet in order to add HTML string inside PDF using HtmlFragment.

Document doc = new Document();
Page currpage = doc.Pages.Add();
HtmlFragment htmlFrag1 = new HtmlFragment(File.ReadAllText(dataDir + "input1.html"));
currpage.Paragraphs.Add(htmlFrag1);
doc.Save(dataDir + "HtmlFrag_out.pdf");

For more information, you may read “Add HTML String using DOM” article in API documentation. In case if you still face any issue or you have any further query regarding DOM approach, please feel free to let us know.


Best Regards,
Asad Ali

Hi Asad,

Thanks for your reply.

I have modified my code as below but still getting the same error as described earlier.

Document doc = new Document();
Aspose.Pdf.Page currpage = doc.Pages.Add();
HtmlFragment htmlFrag1 = new HtmlFragment(“

Hi

”);
currpage.Paragraphs.Add(htmlFrag1);
doc.Save(“HtmlFrag_out.pdf”);

Do not know if I am missing something.

Also my requirement is not to convert any Html file to PDF rather I want to convert a div.html() to a pdf.

Many thanks in advance for your kind help.

Regards,
Bidisha Chakraborty

@asad.ali

Hi Asad,

Thanks for your reply.

I have modified my code as below but still getting the same error as described earlier.

Document doc = new Document();
Aspose.Pdf.Page currpage = doc.Pages.Add();
HtmlFragment htmlFrag1 = new HtmlFragment("

Hi

");
currpage.Paragraphs.Add(htmlFrag1);
doc.Save(“HtmlFrag_out.pdf”);
Do not know if I am missing something.

Also my requirement is not to convert any Html file to PDF rather I want to convert a div.html() to a pdf.

Many thanks in advance for your kind help.

Regards,
Bidisha Chakraborty

@asad.ali
Hi Asad,

I believe the problem is with the SharePoint 2013.

I am using the below code.
// The path to the documents directory.
string dataDir = @“C:”;
// Instantiate Document object

            Document doc = new Document();
            // Add a page to pages collection of PDF file
            Aspose.Pdf.Page page = doc.Pages.Add();
            // Instantiate HtmlFragment with HTML contnets
            HtmlFragment titel = new HtmlFragment("<div><p>Hi Bidisha</p></div>");
            // Set bottom margin information
            titel.Margin.Bottom = 10;
            // Set top margin information
            titel.Margin.Top = 200;
            // Add HTML Fragment to paragraphs collection of page
            page.Paragraphs.Add(titel);

            dataDir = dataDir + "AddHTMLUsingDOM_out.pdf";
            // Save PDF file
            doc.Save(dataDir);

In case od ASP.NET console application the document is getting saved in C folder,
But SharePoint is still giving me the same error.

Is there any method which download the file rather than saving it in a location?

@bidisha.c1

Thanks for providing more details.

In order to download a PDF from browser, without saving it, please check “Sending Pdf to Browser for Download” article in our API documentation. However concerning to the error which you are facing, would you please share complete error description or screenshot of the error window, so that we can further test the scenario in our environment and address it accordingly.

Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
doc.Pages.Add().Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(“Hello World”));
MemoryStream ms = new MemoryStream();
doc.Save(ms);
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.Charset = “UTF-8”;
Response.AddHeader(“content-length”, ms.Length.ToString());
Response.AddHeader(“content-disposition”, String.Format(“attachment;filename=TestDocument.pdf”, “FileName”));
Response.ContentType = “application/pdf”;Response.BinaryWrite(ms.ToArray());
Response.Flush();
Response.End();
Is this Response is from HttpResponse?

@asad.ali

Thanks for your help.
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
doc.Pages.Add().Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(“Hello World”));
MemoryStream ms = new MemoryStream();
doc.Save(ms);
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.Charset = “UTF-8”;
Response.AddHeader(“content-length”, ms.Length.ToString());
Response.AddHeader(“content-disposition”, String.Format(“attachment;filename=TestDocument.pdf”, “FileName”));
Response.ContentType = “application/pdf”;Response.BinaryWrite(ms.ToArray());
Response.Flush();
Response.End();

Is this Response is from HttpResponse?

@bidisha.c1

Thanks for writing back.

Yes, this is HttpResponse object which is used to be associated with Page object and it allows you to send HTTP response to a client. In case of any further assistance, please feel free to ask.

I am getting this exception on this line

doc.Pages.Add().Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(“Hello World”));

{“Could not load file or assembly ‘Aspose.Font, Version=17.1.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56’ or one of its dependencies. The system cannot find the file specified.”:“Aspose.Font, Version=17.1.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56”}

Please help.

@asad.ali

I am getting this exception on this line

doc.Pages.Add().Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(“Hello World”));

{“Could not load file or assembly ‘Aspose.Font, Version=17.1.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56’ or one of its dependencies. The system cannot find the file specified.”:“Aspose.Font, Version=17.1.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56”}

Please help.

@bidisha.c1

Thanks for contacting support.

Please note that it is always recommended to use latest version of the API as it contains all fixes and enhancements. Please try again with latest version of the API which is Aspose.Pdf for .NET 17.7 and in case if you still face any issue, please let us know.

@asad.ali

Thanks for reply.
It is working now.But I lack the CSS part in my DOM controls.I have gone through many airticles in Aspose and got to know that my div must have inline CSS for this.

Could you kindly confirm me if there is no way other than the InLine style approach to get the styling?

Regards,
Bidisha

@bidisha.c1

Thanks for writing back.

We have already logged an investigation ticket as PDFNET-38028, in our issue tracking system for the feature of using external CSS style in HtmlFragment. Our product team will investigate the feasibility of the feature and as soon as we receive some updates in this regard, we will certainly inform you. Please be patient and spare us little time.

We are sorry for the inconvenience.

Hi Asad,

Any update on the issue?

Regards,
Bidisha

@bidisha.c1

Thanks for your inquiry.

I am afraid that earlier logged investigation ticket is still pending for review, as there are other reported issues in the queue which were logged previously and product team has been busy in providing fix against them. They will surely plan to investigate the logged ticket as per their development schedule and as soon as we have some news on it, we will share with you.

Please be patient and spare us little time. We are sorry for the inconvenience.

Hi Asad,

I want to add a border to my pdf page.

This is my current code.Could you kindly help?

Aspose.Pdf.License license = new Aspose.Pdf.License();
// Instantiate license file
license.SetLicense(“Aspose.Total.lic”);
// Set the value to indicate that license will be embedded in the application
license.Embedded = true;
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
//create header
TextStamp textStamp = new TextStamp(hdnOrgChartHeader.Value);
//set properties of the stamp
textStamp.TopMargin = 25;
textStamp.LeftMargin = 20;
textStamp.BottomMargin = 20;
textStamp.HorizontalAlignment = HorizontalAlignment.Left;
textStamp.VerticalAlignment = VerticalAlignment.Top;
textStamp.Height = 18;
textStamp.Width = 250;
textStamp.TextState.ForegroundColor = Color.DarkBlue;
textStamp.TextState.Font = FontRepository.FindFont(“Arial”);
Aspose.Pdf.Page page = doc.Pages.Add();
StringWriter sw = new StringWriter();
HtmlTextWriter h = new HtmlTextWriter(sw);
divMarginParentPDF.RenderControl(h);
string str = sw.GetStringBuilder().ToString();
HtmlFragment titel = new HtmlFragment(str);
page.AddStamp(textStamp);
page.Paragraphs.Add(titel);

@bidisha.c1

Thanks for contacting support.

I am afraid that Aspose.Pdf for .NET does not yet support the feature of adding border directly and we have already logged an enhancement request as PDFNET-37770 in our issue tracking system, for the sake of implementation. As soon as the feature is added, we will definitely let you know. Please spare us little time.

However, as a workaround, you may add border to PDF page with the use of Graph object. Please check following code snippet where I have used Graph instance to add border to the PDF page. For your reference, I have also attached an output PDF document.

Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
//create header
TextStamp textStamp = new TextStamp("Hello World!");
//set properties of the stamp
textStamp.TopMargin = 25;
textStamp.LeftMargin = 20;
textStamp.BottomMargin = 20;
textStamp.HorizontalAlignment = HorizontalAlignment.Center;
textStamp.VerticalAlignment = VerticalAlignment.Center;
textStamp.Height = 18;
textStamp.Width = 250;
textStamp.TextState.ForegroundColor = Color.DarkBlue;
textStamp.TextState.Font = FontRepository.FindFont("Arial");
Aspose.Pdf.Page page = doc.Pages.Add();
page.AddStamp(textStamp);

Aspose.Pdf.Drawing.Graph graph = new Aspose.Pdf.Drawing.Graph((float)page.PageInfo.Width, (float)page.PageInfo.Height);
page.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);
page.Paragraphs.Add(graph);
Aspose.Pdf.Drawing.Line bottomline = new Aspose.Pdf.Drawing.Line(new float[] { 10, 10, (float)page.PageInfo.Width - 10, 10 });
Aspose.Pdf.Drawing.Line topline = new Aspose.Pdf.Drawing.Line(new float[] { 10, (float)page.PageInfo.Height - 10, (float)page.PageInfo.Width - 10, (float)page.PageInfo.Height - 10 });
Aspose.Pdf.Drawing.Line rightline = new Aspose.Pdf.Drawing.Line(new float[] { (float)page.PageInfo.Width - 10, 10, (float)page.PageInfo.Width - 10, (float)page.PageInfo.Height - 10 });
Aspose.Pdf.Drawing.Line leftline = new Aspose.Pdf.Drawing.Line(new float[] { 10, 10, 10, (float)page.PageInfo.Height - 10 });
graph.Shapes.Add(topline);
graph.Shapes.Add(bottomline);
graph.Shapes.Add(rightline);
graph.Shapes.Add(leftline);
doc.Save(dataDir + "PageBorder.pdf");

PageBorder.pdf (21.9 KB)

We are sorry for this inconvenience.

Hi Asad,

I am finding a new error on the PDF document while opening in Adobe reader.

PFA the error in the doc image.

Error.png (21.0 KB)

Also kindly note that this issue is very inconsistent.It happens sometimes in my dev environment,never in my qa but always in the staging environment.

Also it is occuring after I have added img to my html structure.

This is my HTML which I am converting to PDF

            <div id="ctl00_ctl45_g_a24d23d9_9576_4358_ad7c_7d387d4ad1b0_ctl00_divParentPDFOnScrren" class="divParentPDF clearfix" style="position:relative; width: 100px; height: 75px; padding: 5px 3px; border-radius: 4px; background:#0091da; margin:auto; text-align:left; box-sizing:border-box; color:#fff;">
                <div class="divParentConnectorPDF" style="position: absolute; height: 24px; bottom: -25px; left: 44px; border: 1px solid transparent;
   border-left-color: #656565;"></div>
                <div id="ctl00_ctl45_g_a24d23d9_9576_4358_ad7c_7d387d4ad1b0_ctl00_divPersonImgPDFOnScreen" class="divPersonImgPDF" style="width:30px; height:30px;">
                    <img src="Img URL" id="ctl00_ctl45_g_a24d23d9_9576_4358_ad7c_7d387d4ad1b0_ctl00_imgPeople" class="imgClsPersonPDF" style="max-width: 100%; max-height:100%;" />
                </div>
                <div id="divPersonInfoPDFOnScreen" class="divClsPersonInfoPDF" style="width: 100%;">
                    <span id="ctl00_ctl45_g_a24d23d9_9576_4358_ad7c_7d387d4ad1b0_ctl00_spnPersonName" style="display: block; font:bold 9px/13px Arial; color: #fff;">Lorem Ipsam</span>
                    <span id="ctl00_ctl45_g_a24d23d9_9576_4358_ad7c_7d387d4ad1b0_ctl00_spnPersonTitle" style="display: block; font:normal 8px/12px Arial; color: #fff;">>Lorem Ipsam</span>
                    <span id="ctl00_ctl45_g_a24d23d9_9576_4358_ad7c_7d387d4ad1b0_ctl00_spnID" style="position:absolute; visibility:hidden"></span>
                </div>
            </div>
        </div>
        <div class="dividerPDF"></div>
        <div class='actualChildContainerOnScreen' style='width:100px; margin:auto;'>
            <div id='divChildPDFOnScreen377' style='position: relative; height: 75px; background: #f5f5f5; border: 2px solid #005eb8; border-radius:4px; padding: 5px; display: inline-block; float:left; width: 100px; margin-right:0px; margin-left: 0px; margin-bottom:50px; box-sizing:border-box; text-align:left;'>
                <div class='divChildConnectorPDFOnSCreen' style='height: 25px; width: 0px; background: none; border: 1px solid transparent; position: absolute; left: 55%; top: -29px; z-index: 0; border-left-color: #656565; border-top-color: transparent;'></div><div class='divChildImgPDF' style='width:30px; height:30px;display:block;'>
                    <img class='imgChildPDF' id='ChildImgPDF' style='max-width:100%; max-height:100%;' src='http:////Web app URL//sites//site name//Style%20Library//Images//6.png' />
                </div>
                <div class='divChildInfoPDFOnScreen' style='width:100%;'>
                    <span id='spnChildNamePDFOnScreen' style='font: bold 9px/13px Arial; color: #005eb8; display:block;'>Lorem Ipsam</span>
                    <span id='spnJobNamePDFOnScreen' style='font: normal 8px/12px Arial; color: #333; display:block;'>Lorem Ipsam</span>
                </div>
            </div>
        </div>

and this is my code

Aspose.Pdf.License license = new Aspose.Pdf.License();
// Instantiate license file
license.SetLicense(“Aspose.Total.lic”);
// Set the value to indicate that license will be embedded in the application
license.Embedded = true;
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
//create header
TextStamp textStamp = new TextStamp(hdnOrgChartHeader.Value);
//set properties of the stamp
textStamp.TopMargin = 25;
textStamp.LeftMargin = 20;
textStamp.BottomMargin = 20;
textStamp.HorizontalAlignment = HorizontalAlignment.Center;
textStamp.VerticalAlignment = VerticalAlignment.Top;
textStamp.Height = 18;
textStamp.Width = 250;
textStamp.TextState.ForegroundColor = Color.DarkBlue;
textStamp.TextState.Font = FontRepository.FindFont(“Arial”);
Aspose.Pdf.Page page = doc.Pages.Add();
StringWriter sw = new StringWriter();
HtmlTextWriter h = new HtmlTextWriter(sw);
myDiv.RenderControl(h);
string str = sw.GetStringBuilder().ToString();
HtmlFragment titel = new HtmlFragment(str);
page.Paragraphs.Add(titel);
page.AddStamp(textStamp);
#endregion
MemoryStream ms = new MemoryStream();
doc.Save(ms);
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.Charset = “UTF-8”;
Response.AddHeader(“content-length”, ms.Length.ToString());
Response.AddHeader(“content-disposition”, String.Format(“attachment;filename=MyOrgChart.pdf”, “FileName”));
Response.ContentType = “application/pdf”; Response.BinaryWrite(ms.ToArray());
Response.Flush();
Response.End();
Regards,
Bidisha

@bidisha.c1

Thanks for contacting support.

We have tested the scenario with your shared code snippet, while using Aspose.Pdf for .NET 17.9 and were unable to notice the mentioned error, while viewing resultant document in Adobe Reader. For your reference, we have attached generated output as well.

MyOrgChart.pdf (195.9 KB)

Please try using latest version of the API and in case if you still face any issue (which is inconsistent), please specify your development environment and complete routine of execution, so that we can try to reproduce the issue in our environment and address it accordingly.