Forum How do I...?

Interpret plain HTML+CSS?

RobertF
Hi, I am completely new to Prince; I only yesterday found out about it and installed a demo copy, so please bear with me if my question is repulsively elementary. I ran Prince on some plain HTML+CSS files, and was surprised to find that it didn't interpret basic HTML tags such as font changes or paragraph alignment, and neither did it interpret simple CSS specifications for body fonts, span fonts, and text alignment (text-align:justify). I was expecting the language that Prince interprets to include basic HTML and CSS, so that I could easily produce, with some extra style instuctions, quality PDFs from existing web pages. Am I doing something wrong, or am I misunderstanding Prince?

Many thanks in advance,
Robert
Lynx
Prince is mainly intended to format XML documents using CSS, it has only partial support for HTML, however lack of complete HTML support does not affect formatting capabilities of Prince. Everything that you can do using presentational HTML, can be done in Prince using CSS (CSS support in Prince is quite strong).

neither did it interpret simple CSS specifications for body fonts, span fonts, and text alignment (text-align:justify)

Strange. If you use XHTML take into account that selectors are case sensitive (BODY, SPAN, P will not match body, span, p). Example of actual code that fails in Prince would help to name exact reason for failure.
RobertF
Lynx wrote:
Example of actual code that fails in Prince would help to name exact reason for failure.


I've put up one of my test files at

http://www.aetherometry.com/test/aethrelcom_test.html

When I run it through Prince, I get neither the font differentiations, nor justification (I do get both when I make a PDF with the Mac OSX Preview).

Many thanks,

Robert
Lynx
The problem that confuses Prince is TEXT/CSS MIME type of style element and upper case selectors. Changing them to lower case solves the problem in Prince.
Just change
 <STYLE TYPE="TEXT/CSS">
BODY,TH,UL,DL,OL {
	font-weight: 300;
	color: black;
	text-align: justify;
}
...
</STYLE>

to
 <STYLE TYPE="text/css">
body,th,ul,dl,ol {
	font-weight: 300;
	color: black;
	text-align: justify;
}
...
</STYLE>

and it will work in Prince. You can consider this as a bug in Prince because MIME types in HTML are case insensitive according to http://www.w3.org/TR/REC-html40/types.html#h-6.7 and element/attribute names are case insensitive too. To avoid similar problems it is better to use strict XHTML and not to rely on presentational HTML elements/attributes.
RobertF
Lynx wrote:
The problem that confuses Prince is TEXT/CSS MIME type of style element and upper case selectors. Changing them to lower case solves the problem in Prince.


I changed the case in keeping with your suggestions, but this didn't seem to have any effect on the Prince output. Maybe I am just using Prince for something it was not meant to do.

With thanks,
Robert
Lynx
I changed the case in keeping with your suggestions, but this didn't seem to have any effect on the Prince output.

Yes, but now you added comments <!-- --> inside style element that basically hide style sheet from XML processor (Prince does not support HTML natively, it is converted to XHTML before being formatted by Prince, hence some HTML tricks cause problems in XHTML, for example placing content in comments entirely removes this content from document tree produced by XML processor). Remove this coments and it will work. If you'd like I can post modified version of your page and PDF output produced by Prince.

Maybe I am just using Prince for something it was not meant to do.

Basically Prince is intended to format XML documents using CSS, it has partial support for XHTML however and can somehow handle HTML by transforming it to XHTML+CSS, but it is better not to rely on HTML2XHTML transformations and use XHTML+CSS directly.
RobertF
Lynx wrote:
Remove this coments and it will work. If you'd like I can post modified version of your page and PDF output produced by Prince.


I removed the comments and it did, indeed, work beautifully. Thank you very, very much.

Robert
mikeday
Hi Robert,

Today we have released Prince 5.1 rev 7, which should fix two of the issues that you encountered when processing HTML documents: MIME types are now case-insensitive, so "TEXT/CSS" will be treated identically to "text/css", and CSS selectors will also be case-insensitive when applied to HTML documents, so "BODY, H1, P" will be treated identically to "body, h1, p".

Please note that CSS selectors are still case-sensitive when applied to XML documents and XHTML documents, as XML is a case-sensitive specification and the case-insensitive behaviour is only appropriate for legacy HTML.

Thanks for reporting these two issues and please let us know if you have any trouble processing HTML documents with Prince! :D

Best regards,

Michael