Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using separate CSS-file to style different html-inputs #146

Closed
steam0 opened this issue Nov 6, 2017 · 13 comments
Closed

Using separate CSS-file to style different html-inputs #146

steam0 opened this issue Nov 6, 2017 · 13 comments

Comments

@steam0
Copy link

steam0 commented Nov 6, 2017

I was wondering if it is possible to use separate CSS-files to style multiple html-documents in the same way.

For now I have had to put the styling inside every HTML-document, but that is a waste since I will have between tens and houndreds of different documents which should all conform to the same stylesheet.

Any suggestions?

@danfickle
Copy link
Owner

Hi @steam0

You can use the standard link in the head section:

<html>
<head>
<link rel="stylesheet" href="style.css"/>
</head>
<body>
<div>
Hello World!
</div>
</body>
</html>

You can also @import from a CSS file. Remember that you may need to provide a base directory if using relative file paths to link to your css.

@steam0
Copy link
Author

steam0 commented Nov 7, 2017

How do I provide a base directory? Any basic example?

@ghost
Copy link

ghost commented Sep 16, 2019

Same question: How to set a base directory? I am getting:

com.openhtmltopdf.load WARNING:: Unable to load CSS from styles.css

@geekonthepc
Copy link

Just been working on this today.

Use baseUrl here to define a base URL.
builder.withHtmlContent(sourceHtml, baseUrl);

@srinivasthutika
Copy link

srinivasthutika commented Mar 10, 2020

when i use link as below (Using 1.0.2) in html
<link rel="stylesheet" type="text/css" href="style.css"/>

I am getting following exception (Any help would be appreciated.)
com.openhtmltopdf.load INFO:: SAX XMLReader in use (parser): com.sun.org.apache.xerces.internal.parsers.SAXParser
com.openhtmltopdf.load INFO:: SAX XMLReader in use (parser): com.sun.org.apache.xerces.internal.parsers.SAXParser
com.openhtmltopdf.load INFO:: The element type "link" must be terminated by the matching end-tag "".
ERROR: 'The element type "link" must be terminated by the matching end-tag "".'
com.openhtmltopdf.exception WARNING:: Unhandled exception. Can't load the XML resource (using TRaX transformer). org.xml.sax.SAXParseException; lineNumber: 23; columnNumber: 4; The element type "link" must be terminated by the matching end-tag "".

com.openhtmltopdf.util.XRRuntimeException: Can't load the XML resource (using TRaX transformer). org.xml.sax.SAXParseException; lineNumber: 23; columnNumber: 4; The element type "link" must be terminated by the matching end-tag "".
at com.openhtmltopdf.resource.XMLResource$XMLResourceBuilder.createXMLResource(XMLResource.java:274)
at com.openhtmltopdf.resource.XMLResource$XMLResourceBuilder.access$100(XMLResource.java:168)
at com.openhtmltopdf.resource.XMLResource.load(XMLResource.java:81)
at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.setDocumentFromStringP(PdfBoxRenderer.java:293)
at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.(PdfBoxRenderer.java:235)
at com.openhtmltopdf.pdfboxout.PdfRendererBuilder.buildPdfRenderer(PdfRendererBuilder.java:63)
at com.openhtmltopdf.pdfboxout.PdfRendererBuilder.run(PdfRendererBuilder.java:39)
at GenerateHtmlToPDF.generateHtmlToPDF(GenerateHtmlToPDF.java:53)
at GenerateHtmlToPDF.main(GenerateHtmlToPDF.java:21)
Caused by: javax.xml.transform.TransformerException: org.xml.sax.SAXParseException; lineNumber: 23; columnNumber: 4; The element type "link" must be terminated by the matching end-tag "".

@srinivasthutika
Copy link

I am getting following exception if i use it as you mentioned. any help would be appreciated.
com.openhtmltopdf.load INFO:: The element type "link" must be terminated by the matching end-tag "".
ERROR: 'The element type "link" must be terminated by the matching end-tag "".'
com.openhtmltopdf.exception WARNING:: Unhandled exception. Can't load the XML resource (using TRaX transformer). org.xml.sax.SAXParseException; lineNumber: 23; columnNumber: 4; The element type "link" must be terminated by the matching end-tag "".

@srinivasthutika
Copy link

Just been working on this today.

Use baseUrl here to define a base URL.
builder.withHtmlContent(sourceHtml, baseUrl);

what is baseURL here means ??

@danfickle
Copy link
Owner

This library expects valid XML by default. This means all tags need a closing tag or be self-closing:

<link href="..." />
<link href="..."></link>

Either of the above are fine. If you have HTML that is not valid XML, you can convert it. The recommended way to do this is use Jsoup library to parse and convert. However, this is not working perfectly due to #445. It will be fixed in the next release.

var jsoupDoc = Jsoup.parse(html);
var w3cDoc = new W3CDom().fromJsoup(jsoupDoc);

builder.withW3cDocument(w3cDoc, baseUrlMaybeNull);

The base url may be null, if not the path is used as the base for resolving relative resources such as images, css stylesheets, etc. It must be a url such as file:///home/dan/some-path/ or http://localhost:8080/some/path/.

@aleks-shbln
Copy link

Using separate resources can be achieved by providing your own FSStreamFactory:

new PdfRendererBuilder()
    .useProtocolsStreamImplementation(new ClassPathStreamFactory(), "classpath")
    .withHtmlContent(htmlContent, "classpath:/html/")
    ...

Full code can be found in this example project

@daniel-shuy
Copy link

Using separate resources can be achieved by providing your own FSStreamFactory:

new PdfRendererBuilder()
    .useProtocolsStreamImplementation(new ClassPathStreamFactory(), "classpath")
    .withHtmlContent(htmlContent, "classpath:/html/")
    ...

Full code can be found in this example project

Would be great if ClassPathStreamFactory could be added to this library!

@siegelzc
Copy link

siegelzc commented Jan 23, 2024

Would be great if ClassPathStreamFactory could be added to this library!

@daniel-shuy
This looks both useful and easy (the best kind of feature). Could you open an issue on this project. Unfortunately, this repository is no longer maintained.

edit: Just realized we haven't enabled Issues on the other repo yet. Working on that. We're only recently transitioning to developing on the other project. see #921

@daniel-shuy
Copy link

@siegelzc didn't know this repository is no longer maintained, thanks!

I'll create a PR. I just realized that its not a straight copy as the code depends on Spring Boot, but can be re-implemented in a way that doesn't depend on Spring Boot

@daniel-shuy
Copy link

@siegelzc created a PR at openhtmltopdf#3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants