I can't add resources with classpath prefix

134 views
Skip to first unread message

Cezary Hadrzyński

unread,
Oct 5, 2023, 4:58:17 PM10/5/23
to Flying Saucer Users
I can't generate pdf using externally loaded css stylesheet. It is generate without any styles.
Images with classpath prefix are not loaded too.
Everything works when I specify absolute path to file (with prefix file://), but this is not acceptable in my app.

My code:
final ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(html);
renderer.getFontResolver().addFontDirectory(properties.fonts(), EMBEDDED);
renderer.layout();
final ByteArrayOutputStream pdf = new ByteArrayOutputStream();
renderer.createPDF(pdf);
write(properties.output(), pdf.toByteArray());

My HTML:
<html lang="pl-PL">
<head>
<title>Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="pl-PL" />
<link href="classpath:templates/css/stylesheet.css" rel="stylesheet" type="text/css" media="print" />
</head>
<body>...</body>
</html>

My maven dependecies:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.4</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>pdf-generator</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>pdf-generator</name>
<description>pdf-generator</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf-openpdf</artifactId>
<version>9.3.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

Per Nyfelt

unread,
Apr 12, 2024, 7:24:30 AM4/12/24
to Flying Saucer Users
There might be a fancier way, but one way to deal with this is to preprocess the html and replace the path to the css with something like `getClass().getResource("/templates/css/stylesheet.css").toExternalForm()` (or getServletContext().getRealPath() if you are in a servlet)
e.g.
final ITextRenderer renderer = new ITextRenderer();
var cssPath = getClass().getResource("/templates/css/stylesheet.css").toExternalForm();
renderer.setDocumentFromString(html.replace("classpath:templates/css/stylesheet.css", cssPath));
renderer.getFontResolver().addFontDirectory(properties.fonts(), EMBEDDED);
renderer.layout();
final ByteArrayOutputStream pdf = new ByteArrayOutputStream();
renderer.createPDF(pdf);
write(properties.output(), pdf.toByteArray());
Reply all
Reply to author
Forward
0 new messages