Convert HTML to PDF in C# Application using Aspose.PDF for .NET

I’m creating a application in c#. As part of the program program I need aspose to convert an .html file to a .pdf that the application can then save to a folder. The html containes tables which need to be accurately converted.
Can aspose do this? What product would I need to purchase? Are there any code examples?
Thank You

@MKNOWLES,

Thanks for contacting support.

Can you please share source files along with sample project so that we may further investigate to help you out.

Hi - We’ve decided it would be better to do this from a classic asp page, rather than from a C# application. The user selects that he wants to create the Purchase Order PDF from our classic asp page intranet. The thougts are to either build the pdf or build it as a .html then convert that to the pdf. We already have the code to generate the html. Can aspose create a .pdf from classic asp code? Also could it render an existing .html file via a .asp

Thanks

@MKNOWLES,

I regret to inform that Aspose.PDF does not support classic ASP. A ticket with ID PDFNET-48344 has been created as in our issue tracking system as a new feature request. We will look into the possibility of implementation of the requested feature. This thread has been associated with this new feature request, so that you can be automatically notified as soon as this issue is resolved.

Hi - Our developer has found this link

which he says does imply Aspose.PDF supports Classic ASP?

Thank You.

@MKNOWLES

You can not use Aspose.PDF API directly but with COM Wrapper, it can be used. For more details, please visit this thread.

Hi - I now have an .html page being producing a .pdf file.
Just have the image being centralised rather than being on the left.

This is the .html code

<page size="A4">
 <div class="clsPageWidthBlock">
	<div class="clsPageBody">
			<div class="clsDocHeader">
				<img class="imgLogo" src="logo_tm_rgb.png">
				<span>Purchase Order</span>
			</div>
			<div class="clsFormHeader">
				<div class="spnAddrBlock">

and the class

Html original .png (2.5 KB)

pdf file .png (32.4 KB)

and i’ve attached the class in a png class.png (3.3 KB)
file as I couldn’t paste it in.

Guess I need to know how Aspose.pdf handles headers and footers that might give me a .clue as to the correct .html coding.

@MKNOWLES

We have tested the scenario in our environment while preparing a sample HTML file having same CSS Class that you shared for image. We were unable to notice any issue in output PDF document as image was rendered on the top-left corner of the PDF page. For your kind reference, attached are input HTML and Output PDF:

inputHtml.zip (331 Bytes)
table.pdf (8.5 KB)

var loadoptions = new HtmlLoadOptions(dataDir);
loadoptions.PageInfo.Width = PageSize.A4.Height;
loadoptions.PageInfo.Height = PageSize.A4.Width;
loadoptions.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);
Document doc = new Document(dataDir + "inputHtml.html", loadoptions);
doc.Save(dataDir + "Table.pdf");

Would you kindly share your complete HTML file with which we can replicate the issue in our environment? We will again test the scenario and address it accordingly.

Hi - I’ve uploaded our test .html file. as a .zip file, and gthe output we were getting.

MBMTEST_PORDER_PP123456.html.pdf (1.0 MB)
MBMTEST_PORDER_PP123456.zip (3.7 KB)

@MKNOWLES

We were able to notice the issue in our environment and logged it as PDFNET-48364 in our issue tracking system for the sake of correction. Furthermore, we also noticed that image was rendered at correct position in PDF when we removed following CSS property from its class:

position: absolute;

It seemed that API was not honoring absolute positioning in the HTML. So you may please try removing above property and perform conversion in order get desired results. We will further check the issue in detail and keep you posted with the status of its correction. Please be patient and spare us some time.

We are sorry for the inconvenience.

        htmlLoadOptions.PageInfo.Width = PageSize.A4.Height;
        htmlLoadOptions.PageInfo.Height = PageSize.A4.Width;
        htmlLoadOptions.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);

added code and get Issues aspose.png (47.6 KB)

Removed the absolute:position Just need the logo moving over a bit now.

The developer said that we need to have the .html file and the .pdf file looking the same.

Also the .pdf file seems to be in Landscape orientation.

@MKNOWLES

We were unable to notice this issue in our environment as you can see the output PDF shared in our previous reply. Furthermore, we have been testing the scenario by referring one of our sample images in your HTML as the link to an image (in your shared PDF) does not work and image does not render in the HTML at our side.

Would you please share some other image URL so that we can again test it at our side.

Please note that the height and width of the output PDF are set in the code according to landscape orientation. You may please remove the following lines from code in order to export PDF in desired page orientation.

htmlLoadOptions.PageInfo.Width = PageSize.A4.Height;
htmlLoadOptions.PageInfo.Height = PageSize.A4.Width;

Furthermore, you can also set different margins in your code to obtain your expected output PDF. However, if issue still persists or you notice any anomaly in output PDF, please share some screenshots showing the differences between input/output. We will further proceed to assist you accordingly.

Hi - Still getting the .html page going onto a second page

        var htmlLoadOptions = new HtmlLoadOptions(fileInfo.DirectoryName);
        htmlLoadOptions.PageInfo.Margin.Left = 0;
        htmlLoadOptions.PageInfo.Margin.Right = 0;
        htmlLoadOptions.PageInfo.Margin.Top = 0;
        htmlLoadOptions.PageInfo.Margin.Bottom = 0;

Overflow.png (74.4 KB)

We changed the .html code, and the image issue we had is now correctly. Just this last issue for getting the page to display on one page.

@MKNOWLES

We tried to increase the PDF Page height in order to fit the content on single page but, we noticed that a blank page is being added in the output PDF.

HtmlLoadOptions objLoadOptions = new HtmlLoadOptions(dataDir);
objLoadOptions.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);
objLoadOptions.PageInfo.Height = PageSize.A4.Height + 100;
Aspose.Pdf.Document doc = new Aspose.Pdf.Document(dataDir + "MBMTEST_PORDER_PP123456.html", objLoadOptions);
doc.Save(dataDir + "htmltopdf.pdf");

htmltopdf.pdf (153.6 KB)

Hence, we have logged an issue as PDFNET-48408 in our issue tracking system. We will further check this behavior of the API in details and keep you posted with the status of ticket resolution. Please be patient and spare us some time.

We are sorry for the inconvenience.

Hi - Thank You, do you know the average resolution time for this? I realise that’s quite a hard question to answer!

@MKNOWLES

The issues are resolved on first come first serve basis in normal support model and resolution time of an issue may depend upon its complexity and required work to be done to get it fixed. However, we will surely keep you informed as soon as we make some significant progress towards issue resolution. Please spare us some time.

We are sorry for the inconvenience.

PS: As a workaround, you can remove/delete extra blank Page in output PDF generated by shared code snippet.

Hi - I have added the code to remove the extra page. Seems to work well.

@MKNOWLES

It is good to know that suggested workaround worked for you. Please also check another following approach to render all HTML content on single Page of the PDF instead of specifying height:

string outFile = "out.pdf";
HtmlLoadOptions options = new HtmlLoadOptions();
options.IsRenderToSinglePage = true;
Document doc = new Document("HTML/input.htm", options);
doc.Save(outFile); 

The issues you have found earlier (filed as PDFNET-48364) have been fixed in Aspose.PDF for .NET 24.2.