HTML to PDF. Layout ('top' CSS tag) on text in divs are not working

Hi,


I am having a problems with a block of code and I can’t figure out how to resolve it.

What I wanted was the
to have a background image and then I could place text within the div. I don’t think Aspose PDF has this functionality (which would be useful).

I therefore tried a different method (see below):

HTML:

Outline



CSS:

div.imagebk
{
position: relative;
width: 1115px;
height: 788px;
margin-left: 0px;
margin-top: 0px;
margin-bottom: 0px;
margin-right: 0px;
text-align:left;
}

p.titleheading
{
position: absolute;
top: 20px;
left: 60px;
width: 1115px;
font-family: Calibri;
font-size: 90pt;
color: #ffffff;
}

The above sort of works but the PDF generator is not registering the CSS ‘top’ tag.
The CSS ‘left’ tag seems to be working fine, but I have tried and tried and tried again but I can’t get it working.

I have attached a picture to illustrate my point. This is what is generated on the output. As you can see it has moved left but not moved down.

It is really annoying and I need some urgent help in fixing it.

Kind Regards,
Will

Hi Will,


Thanks for your inquiry. Please use new DOM approach for HTML to PDF conversion. I have tried your HTML with a sample image and it is honoring top tag. Hopefully it will help you to accomplish the task.

// Specify the The base path/url for
the html file which serves as images database
<o:p></o:p>

String basePath = "E:/Data/";

HtmlLoadOptions htmloptions = new HtmlLoadOptions(basePath);

// load HTML file

Document doc = new Document("E:/Data/Test_image.html", htmloptions);

//Document doc = new Document(inputStream, htmloptions);

doc.Save("E:/Data/HTMLtoPDFDOM1.pdf");

Please feel free to contact us for any further assistance.


Best Regards,

Hi,

Thanks for your reply.

Okay I will try and see if I can use the DOM approach.

Just a couple of questions…
Is there no way of just passing an HTML string to a new document and saving it that way?
Furthermore I need to force new pages, how do you force new pages using the DOM approach?

I need to have headers and footers AND a way of saying this is page 1 of 2 etc etc.
All of the above currently allows me to do this using the old way of creating PDFs. If the DOM approach cannot do this, I can’t use it and therefore need a solution to my original query.

Will also the background img work for DIVs in DOM or is that not developed yet?

Just for your information I currently at the moment save the PDF in a memory stream (not using DOM) and then load it in as a document to compress the PDF file.

Kind Regards,
William

I have tried to convert my code into DOM and it is turning out to be a nightmare.
Please help me and point me in the right direction. See my questions above this message too.

I keep getting this error: 'The rectangle size can not be larger page size’

The code that I using is below. It loops around a page list that I setup which has HTML pages as strings (item.HtmlCode) and other page parameters for each page. Lots of pages have images the size of the pages, so I made the page size bigger but still it keeps bringing this error.
I really don’t know if I am doing this right because I am having major trouble finding things that I need in the documentation.

’ Instantiate Document object
Dim doc As Document = New Document()

For Each item As PageList In HTMLPageList

’ Add a page to pages collection of PDF file
Dim page As Page = doc.Pages.Add()

If item.Orientation = PageOrientation.Portrait Then
'page.SetPageSize(795, 1124)
page.PageInfo.Width = 800
page.PageInfo.Height = 1200
page.SetPageSize(800, 1200)

ElseIf item.Orientation = PageOrientation.Landscape Then
page.PageInfo.IsLandscape = True
page.PageInfo.Width = 1200
page.PageInfo.Height = 800
page.SetPageSize(1200, 800)
'page.SetPageSize(1124, 795)
End If

’ Instantiate HtmlFragment with HTML contnets
Dim titel As HtmlFragment = New HtmlFragment(item.HtmlCode)

’ Add HTML Fragment to paragraphs collection of page
page.Paragraphs.Add(titel)

Next

’ Save PDF file
doc.Save(saveFilePath)


Furthermore why can’t I use preset sizes like A4 or A3 like the old generator?

Hi William,

William1991:


Is there no way of just passing an HTML string to a new document and saving it that way?

Yes you can add HTML string to PDF document using HtmlFrgment. Please check this documentation link to create a PDF document using HTML string.


William1991:

Furthermore I need to force new pages, how do you force new pages using the DOM approach?
I need to have headers and footers AND a way of saying this is page 1 of 2 etc etc.
All of the above currently allows me to do this using the old way of creating PDFs. If the DOM approach cannot do this, I can't use it and therefore need a solution to my original query.

Please find attached code snippet to create PDF document from scratch using DOM with Header/Footer. Contents will automatically flow to new page. However if you want to force start from new page. You can create another Page object for the purpose.

William1991:

Will also the background img work for DIVs in DOM or is that not developed yet?

Just for your information I currently at the moment save the PDF in a memory stream (not using DOM) and then load it in as a document to compress the PDF file.


You can use new DOM approach for HTML to PDF conversion, it supports the div with background image.

Please feel free to contact us for any further assistance.

Best Regards,