baseUri in withHtmlContent not working as expected
NehalDamania opened this issue · 1 comments
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.
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.