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

Free Support Forum - aspose.com

Formatting HTML element in PDF

I am passing entire HTML string through AJAX POST call in HTML and hitting ASPX web method to convert the HTML string (retrieve through jquery.html()) to PDF. It is converting to PDF through below code, but in very poor format (like the textbox going totally out of page. Image is not loading). If I directly click print option in html and convert that to PDF through any tool, it is giving exact format. Can you please help me to get the code to set the exact alignment. Please also notice the code using other DLL. with these minimal code, it is converting it to the closed format with minor issue.

[WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static string Test(string strHTML)
{
string msg = “PDF is ready.”;
string configvalue1 = “”;
HttpRequest Request = HttpContext.Current.Request;
try
{
string baseUrl = Request.Url.Scheme + “://” + Request.Url.Authority +
Request.ApplicationPath.TrimEnd(’/’) + “/HTMLFolder/”;

Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();
pdf.HtmlInfo.CharSet = “UTF-8”;
Aspose.Pdf.Generator.Section section = pdf.Sections.Add();
Aspose.Pdf.Generator.Text text2 = new Aspose.Pdf.Generator.Text(section, strHTML);
text2.IsHtmlTagSupported = true;
text2.IsHtml5Supported = true;
section.Paragraphs.Add(text2);
text2.IfHtmlTagSupportedCssWinsOnFirstLevelChildren = true;
pdf.IsImageNotFoundErrorIgnored = true;
pdf.Security = new Aspose.Pdf.Generator.Security();
pdf.Security.IsFormFillingAllowed = false;
//pdf.PageSetup.PageWidth = 1200;
configvalue1 = ConfigurationManager.AppSettings[“output”] +
(HttpContext.Current.User.Identity).Name.Substring(7) +
“" + (HttpContext.Current.Request.UrlReferrer.Segments[3]) +
"” + DateTime.Now.ToString(“yyyyMMddHHmmss”) + “.pdf”;
pdf.Save(configvalue1 );
}
catch (Exception ex)
{
msg = “Some Error in calling Service.” + ex.Message;
}
//SelectPdf.HtmlToPdf converter = new SelectPdf.HtmlToPdf();
//SelectPdf.PdfDocument doc = converter.ConvertHtmlString(strHTML, baseUrl);
//doc.Save(@“C:/XXX/XXXXXXX/WebSite1/Output/test.pdf”);

return msg + " PDF File is present in machine " + Request.Url.Authority +" at "+ configvalue1;//return what was sent from the client to the client again
}//end get value

Hi,


Thanks for contacting support. I am testing the scenario with my sample HTML string and will get back to you with my findings, shortly.

Best Regards,

Hi,


Thanks for contacting support.

Adding more to Asad’s comments, in order to accurately test the scenario, we request you to please share the input HTML and sample project, so that we can test the scenario in our environment.

Hi,


Thanks for your patience.

I have tested the scenario in our environment with a sample HTML string using Aspose.Pdf for .NET 17.4.0 and did not notice any issue. As requested above, please share your input HTML string, so that we can test the scenario in our environment and address it accordingly.

We are sorry for the inconvenience.


Best Regards,

HI,


Please find the HTML page. Please copy the entire HTML and pass them as string. Please select the drop down Other option in the page.

Hi,


Thanks for sharing HTML. I am looking into the scenario and will get back to you shortly. Please be patient.

Best Regards,

Hi,


Thanks for your patience.

I have tested the scenario with the HTML which you have shared, using Aspose.Pdf for .NET 17.4.0, by following code snippet and I did not notice any issue. Please check in the following code snippet that I have used latest DOM (Document Object Model) approach because old Aspose.Pdf.Generator model is going to be obsolete soon and it is recommended to use latest approach.


[WebMethod]<o:p></o:p>

[ScriptMethod]

public static string Test(string strHTML)

{

string msg = "PDF is ready.";

string configvalue1 = "";

HttpRequest Request = HttpContext.Current.Request;

try

{

File.WriteAllText(HttpContext.Current.Server.MapPath(@"App_Data/HtmlToPdf.html"), strHTML);

Aspose.Pdf.Document doc = new Aspose.Pdf.Document();

Aspose.Pdf.HtmlLoadOptions loadoptions = new Aspose.Pdf.HtmlLoadOptions();

loadoptions.PageInfo.IsLandscape = true;

doc = new Aspose.Pdf.Document(HttpContext.Current.Server.MapPath(@"App_Data/HtmlToPdf.html"), loadoptions);

doc.Save(HttpContext.Current.Server.MapPath(@"App_Data/HtmlToPdf2.pdf"));

}

catch (Exception ex)

{

msg = "Some Error in calling Service." + ex.Message;

}

return msg + " PDF File is present in machine " + Request.Url.Authority + " at " + configvalue1;

}



As you can see in the highlighted part of code that I am saving all HTML string as file in the directory and converting that file into PDF later. I have attached an output for your reference as well. Please try using this approach to convert the HTML of entire page into PDF and in case if you still face any issue, please feel free to contact us.


Best Regards,

Thanks for the reply and appreciate your help to sort out the issue. I am getting below issues.

(1) Error 137 ‘Aspose.Pdf.HtmlLoadOptions’ does not contain a definition for ‘PageInfo’ and no extension method ‘PageInfo’ accepting a first argument of type ‘Aspose.Pdf.HtmlLoadOptions’ could be found (are you missing a using directive or an assembly reference?) C:XXX\XXX\XX.cs 73 29 C:…\WebSite1</div>

(2.). The javascript get executed multiple time and creating duplicate set of fields at top.

(3). Fonts also seems differnt.

Can you please help to address these issues.
vicage:
Thanks for the reply and appreciate your help to sort out the issue. I am getting below issues.
(1) Error 137 'Aspose.Pdf.HtmlLoadOptions' does not contain a definition for 'PageInfo' and no extension method 'PageInfo' accepting a first argument of type 'Aspose.Pdf.HtmlLoadOptions' could be found (are you missing a using directive or an assembly reference?) C:XXX\XXX\XX.cs 73 29 C:\...\WebSite1\
Hi,

Thanks for sharing the details.

Can you please share the details regarding the version of Aspose.Pdf for .NET you are using ?

Hi,


Thanks for writing back.

vicage:
(2.). The javascript get executed multiple time and creating duplicate set of fields at top.

As issue seems related to your application, not with Aspose.Pdf API, so we will really appreciate if you please share a sample web application which reproduces the issue, so that we can understand the issue precisely and address it accordingly.

vicage:
(3). Fonts also seems differnt.

I did not notice any issue in our environment, with the output which I also had shared with you in my earlier reply. As shared above, please share the version of the API which you are using to perform the functionality, so that we can provide feedback appropriately.


Best Regards,