HTML to PDF conversion - set pdf page size to one page

I am converting a HTML document to a PDF.
How do I specify to create a one page PDF ? (fit the whole HTML document on one PDF page.)

The code below specifies a fixed width and height.
How do I create a PDF that will always be one page, the length depending on the length of the HTML page being converted? (The length of the HTML doc varies.)

        Dim document = New HTMLDocument(newhtmlfileOutOfProject)
        Dim options = New Aspose.Html.Saving.PdfSaveOptions()
        options.PageSetup.AnyPage = New Aspose.Html.Drawing.Page(
                     New Aspose.Html.Drawing.Size(Aspose.Html.Drawing.Unit.FromInches(8.5),
                   Aspose.Html.Drawing.Unit.FromInches(11)),
                   New Aspose.Html.Drawing.Margin(0))
        Aspose.Html.Converters.Converter.ConvertHTML(document, options, strDestinationFilePDF)

@shif

A feature request as HTMLNET-3225 has been logged in our issue tracking system in order to add different sizing options. We have linked the ticket ID with this thread so that you will receive notification as soon as the feature is added to the API. Please be patient and spare us some time.

We are sorry for the inconvenience.

  1. Is there a way to get the length/height of an Aspose HTML document?
    I would then use that value to calculate the height of the pdf.
    This must be done dynamically because each html page is a different size.

  2. If the pdf created is more than one page, the layout of the second page is not correct.
    Is there code you can send me that will create a PDF from an html doc that will produce the second page with the correct layout as seen in the html doc. The css rules are not applied correctly for the second page.

Thank you

@shif

We are afraid that currently, we do not provide the ability to calculate the document size. And even if we did, it would not help. Most of the HTML documents are page-size dependent. As an example, the body element can have width=120% and it will always be wider than the page.

Could you please share a sample HTML for our reference so that we can also observe the same issue in our environment and address it accordingly?

I would like to attach the sample html file as per your request.
How can I do that?

@shif

You can add it to .zip format and attach to your post using the upload button in post editor.

htmlsupporttest.zip (5.9 KB)

SupportTestPDF.zip (57.3 KB)

.Attached is a zip with the html file and a zip file with the file converted to PDF.
Notice the second page of the PDF file generated is not displayed correctly.

Thanks

@shif

We were able to reproduce the issue in our environment while testing the scenario with latest version of the API. Hence, an issue as HTMLNET-3233 has been logged in our issue tracking system for the sake of correction. We will further look into its details and let you know once it is rectified. Please be patient and spare us some time.

We are sorry for the inconvenience.

I understand that Aspose should be able to convert an HTML file to PDF.
That was the purpose of buying the license.
Can you please send me code that will convert the html file that I sent you to PDF.
The PDF including the second page should display as it appears in the html file.

Thank you.

@shif

We are afraid that we could not fully understand your inquiry. The code to convert HTML to PDF has already been shared in this forum thread in your first post. We also used the same code snippet for testing. Please clarify what else functionality you want to perform using the API?

The code that was used created a pdf with the second page not displaying correctly.
You therefore logged an issue. Do you have code that would convert the html file that I sent to PDF correctly? There are many ways to convert Html to PDf in Aspose and I’d like to see code that accomplished that.
The api should be able to do that. Please let me know If the Aspose API cannot convert the html to PDF correctly. It does not have to be on one page. Each page can be an A4.

Thank you.

@shif

We tried using both Aspose.PDF and Aspose.Words (as they both offer HTML to PDF Conversion) but, output PDF was not correct. Please also note that Aspose.HTML is specialized to deal with HTML documents and we are already working on it to improve the conversion feature.

Furthermore, we tried to convert the HTML to a Single Page PDF document, and the output result was better using Aspose.PDF. Please check the below code snippet and attached output PDF:

Aspose.Pdf.HtmlLoadOptions objLoadOptions = new Aspose.Pdf.HtmlLoadOptions();
objLoadOptions.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);
objLoadOptions.IsRenderToSinglePage = true;
Aspose.Pdf.Document doc = new Aspose.Pdf.Document(dataDir + "htmlsupporttest.html", objLoadOptions);
doc.Save(dataDir + "test.pdf", SaveFormat.Pdf);

test.pdf (155.9 KB)

Thank you for sending that code.
It works if I don’t have custom fonts referenced in the style sheet of the html page.
I get the error ’ Font embedding is prohibited because of font license restrictions* if I convert the html t to PDF using Apsose.PDf as you do above.
If I use Aspose.Html.Converters.Converter.ConvertHTML, I do not get an error and the fonts show up correctly.
How can I get the fonts or at least not get the error when using the code you sent using Aspose.PDF?
(I do not want to use the Aspose.Html because radio buttons do not show when opening the PDF in Acrobat reader.)

Thanks

@shif

With Aspose.PDF, please try to use the Document.DisableFontLicenseVerifications = true; property after initializing the Document as below in order to prevent the fonts related exception:

Aspose.Pdf.Document doc = new Aspose.Pdf.Document(dataDir + "htmlsupporttest.html", objLoadOptions);
doc.DisableFontLicenseVerifications = true;

I get the error ‘Font embedding is prohibited because of font license restrictions’ already on the first line you sent, the line that creates a new PDF document using the html page. The html page has the custom fonts so it already generates an error.

My code now:
Dim doc As Aspose.Pdf.Document = New Aspose.Pdf.Document(htmlfilename, objLoadOptions)
doc.DisableFontLicenseVerifications = True

Is there a way to create the pdf file with no parameters, set the DisableFontLicenseVerifications to true,
and then add the html as the contents of the PDF document.

   Dim doc As Aspose.Pdf.Document = New Aspose.Pdf.Document()  'no parameters
    doc.DisableFontLicenseVerifications = True

Can you send me the code to convert the contents of the htm file and add it to doc - the PDF document.
Perhaps that would be a solution.

Thank you.

I tried the following code to add the html to the PDF created with no parameters. I get no errors. The fonts show correctly, but the PDF does NOT show correctly. (Radio buttons are in the wrong place, values of textboxes are in the wrong place etc.)
Can you let me know how I can create the PDF that will render the html correctly.

             Dim doc As Aspose.Pdf.Document = New Aspose.Pdf.Document()
            doc.DisableFontLicenseVerifications = True
            Dim currpage As Page = doc.Pages.Add()
            Dim htmlFrag1 As HtmlFragment = New HtmlFragment(File.ReadAllText(newhtmlfileOutOfProject))
            currpage.Paragraphs.Add(htmlFrag1)
            doc.Save(strDestinationFilePDF)

Thank you.

@shif

Can you please share your sample HTML page in .zip format with us? We will test the scenario in our environment and address it accordingly.

Attached is an html file .zip format.
Can you send me other code that will generate the PDF correctly.
I will need it to work with the custom fonts also.

Thank you.
htmlsupporttest.zip (5.8 KB)

@shif

While using the above code snippet with your recently shared HTML file, we did not notice any exception at our end. The generated output PDF is also attached for your reference. Furthermore, could you please make sure that all Windows Fonts are installed at your side properly. Also, please share complete environment details where you are using the API so that we can again try to reproduce the issue you are facing and share our feedback with you.

test.pdf (155.9 KB)

In the post above, you suggested that I use the following to avoid the error from the custom fonts.
Aspose.Pdf.Document doc = new Aspose.Pdf.Document(dataDir + “htmlsupporttest.html”, objLoadOptions);
doc.DisableFontLicenseVerifications = true;

The first line already generated an error when trying to create a doc from the html that contains the custom fonts, so I created a PDF document with no parameters and did the following to add the html to the document.I did not get error, but It did not render the html to the PDF correctly.

Here is the code:

Dim doc As Aspose.Pdf.Document = New Aspose.Pdf.Document()
doc.DisableFontLicenseVerifications = True
Dim currpage As Page = doc.Pages.Add()
Dim htmlFrag1 As HtmlFragment = New HtmlFragment(File.ReadAllText(newhtmlfileOutOfProject))
currpage.Paragraphs.Add(htmlFrag1)
doc.Save(strDestinationFilePDF)

The code you sent me in your last reply renders the html to PDF beautifully, but gives an exception if there are custom fonts. Please test the html file I sent you with this code. The file does not include custom fonts.
The code I sent you should work for custom fonts, but does not even convert from html to PDF without the custom fonts.

Thank you.