How to fit HTML to a page when converting HTML to PDF

1 Answer 41 Views
PdfProcessing WordsProcessing
Kaya
Top achievements
Rank 1
Kaya asked on 29 Jun 2021, 08:32 PM

When writing an application to convert HTML documents to PDF documents on the server side, my HTML document gets resized and becomes weirdly formatted. The before and after conversion is depicted below. I believe its because the original HTML page is much larger than the resulting PDF, causing some formatting issues when it is squished. Is there a way I can resize the HTML page before it is added to the PDF so that this does not happen? Or does anyone have any ideas about what is causing this in the first place?

It is not a problem with importing the HTML file itself, as importing to HTML and exporting back to HTML leaves a near identical result.

Thank you for your help.

 

1 Answer, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 30 Jun 2021, 09:03 AM

Hi Kaya,

The page has a default size when the document is imported (in HTML the page width usually depends on the available size). You can set this with the following code: 

var provider = new HtmlFormatProvider();
var document = provider.Import(File.ReadAllText("sample.html"));

foreach (var section in document.Sections)
{
    section.PageSize = new System.Windows.Size(1600, 2000);
}
//export to pdf omitted

Let me know if I can assist you further.

Regards,
Dimitar
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Kaya
Top achievements
Rank 1
commented on 30 Jun 2021, 08:25 PM

Thank you! I tried that and it fixed the sizing errors. However, I am still needing to get rid of the borders that are automatically added when importing using the HTML Format provider. My original document does not have "border" css tags in the style attributes, but it does when being converted to a PDF. How should I get rid of these tags that are automatically added to my HTML when I import it?

I took a look at the DefaultStyleSheet property of HTMLImportSettings, but it doesn't seem like I can just remove the import settings, and every time I try to enter a custom style sheet, it causes the exception "Key not found." The documentation isn't very clear on how I should set the import settings. Do you have an idea about how this works or whether it is the solution I am looking for?

Thank you.
Dimitar
Telerik team
commented on 01 Jul 2021, 10:16 AM

Hi Kaya,

I am not sure where the undesired borders are coming from. I can see that you are entitled to the support service. This is why I want to kindly ask you to submit a support ticket which is a private thread. This way you can attach your HTML and CSS. This will allow us to test your case on our side and determine what is causing such behavior.

Thank you in advance for your patience and cooperation.

Tags
PdfProcessing WordsProcessing
Asked by
Kaya
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or