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

baseUri in withHtmlContent not working as expected #566

Closed
NehalDamania opened this issue Oct 4, 2020 · 1 comment
Closed

baseUri in withHtmlContent not working as expected #566

NehalDamania opened this issue Oct 4, 2020 · 1 comment

Comments

@NehalDamania
Copy link

When we use

 String baseUri = new ClassPathResource("templates").getURL().toExternalForm();
 System.out.println("baseUri :" + baseUri);
 PdfRendererBuilder builder = new PdfRendererBuilder();
 builder.useFastMode()
	.withHtmlContent(html, baseUri)

The "templates" folder is under "src/main/resources". And, the templates folder contains the HTML, CSS and fonts

However, when searching for fonts and css file, openhtmlpdf searches under classes folder instead of templates and gives the error as such

com.openhtmltopdf.load INFO:: Requesting stylesheet: file:/D:/development/project-1/target/classes/resume.css
com.openhtmltopdf.exception WARNING:: Item at URI file:/D:/development/project-1/target/classes/resume.css not found

The baseUri is printed correctly as

baseUri: file:/D:/development/project-1/target/classes/templates

I assumed it should have used the templates folder as base as below.
D:/development/project-1/target/classes/templates/resume.css

Same errors for fonts, they are not found as they are inside src/main/resources/templates
Now, if I move all my html, css and fonts directly under src/main/resources folder, everything works fine.
So, somehow the baseUri seems not to be working as expected.

@NehalDamania NehalDamania changed the title baseUri in withHtmlContent is not working baseUri in withHtmlContent not working as expected Oct 4, 2020
@NehalDamania
Copy link
Author

Ok. Got it
Changing the following code made it work perfectly
Changed from

String baseUri = new ClassPathResource("templates").getURL().toExternalForm();

to

String baseUri = new ClassPathResource("templates/resume.html").getURL().toExternalForm();

Now, it is searching the correct path for other resources. Might help someone if stuck.

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

1 participant