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

Make it possible to add resources with classpath: prefix #216

Closed
lowcasz opened this issue Oct 5, 2023 · 12 comments · Fixed by #241
Closed

Make it possible to add resources with classpath: prefix #216

lowcasz opened this issue Oct 5, 2023 · 12 comments · Fixed by #241
Assignees
Labels
Milestone

Comments

@lowcasz
Copy link

lowcasz commented Oct 5, 2023

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>
@asolntsev
Copy link
Contributor

@lowcasz Did it ever work? Do you mean that FlyingSaucer did load resources from classpath, and this feature was broken with some release? Or it never really worked, and you just wish such a feature?

@lowcasz
Copy link
Author

lowcasz commented Oct 6, 2023

It was worked on my other project.
HTML is copied from old project for test, it was work there.
I don't see any other configuration and configuration classes between them.
In old project is older version of saucer. (9.1.22)

@pbrant
Copy link
Member

pbrant commented Oct 6, 2023 via email

@lowcasz
Copy link
Author

lowcasz commented Oct 6, 2023

I don't know, maybe...
Do you know any dependency, especially spring dependency which can do it in background?
It can helps me to find it, I don't see any additional parameters, configuration or classes which do it.

@pbrant
Copy link
Member

pbrant commented Oct 6, 2023 via email

@asolntsev asolntsev added this to the 9.3.2 milestone Dec 28, 2023
@asolntsev asolntsev self-assigned this Dec 28, 2023
asolntsev added a commit that referenced this issue Dec 28, 2023
now it's possible to use "classpath:" prefix in URLs of included resources, e.g.
```
<link href="classpath:styles/sample.css"/>
```
@asolntsev asolntsev linked a pull request Dec 28, 2023 that will close this issue
@asolntsev asolntsev changed the title I can't add resources with classpath prefix Make it possible to add resources with classpath: prefix Dec 28, 2023
asolntsev added a commit that referenced this issue Dec 28, 2023
now it's possible to use "classpath:" prefix in URLs of included resources, e.g.
```
<link href="classpath:styles/sample.css"/>
```
@asolntsev
Copy link
Contributor

asolntsev commented Dec 28, 2023

@lowcasz implemented in #241, released version 9.3.2 today.

@andrzej-talarek
Copy link

Funny thing, but in my case it worked perfectly fine (classpath:/static/css/custom.css) in 9.3.1 version and broke with 9.3.2 :(. It's broken both for bootJar task, that I'm using for system development usually and normal building jar for running it with java -jar. Can't upgrade.

2023-12-31 06:18:15.880 ERROR -  o.a.j.l.DirectJDKLog : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.NullPointerException: Cannot invoke "java.net.URL.toString()" because "resource" is null] with root cause
java.lang.NullPointerException: Cannot invoke "java.net.URL.toString()" because "resource" is null
	at org.xhtmlrenderer.swing.NaiveUserAgent.resolveURI(NaiveUserAgent.java:370)
	at org.xhtmlrenderer.context.StyleReference.getStylesheets(StyleReference.java:224)
	at org.xhtmlrenderer.context.StyleReference.setDocumentContext(StyleReference.java:85)
	at org.xhtmlrenderer.pdf.ITextRenderer.setDocument(ITextRenderer.java:183)
	at org.xhtmlrenderer.pdf.ITextRenderer.setDocument(ITextRenderer.java:144)
	at org.xhtmlrenderer.pdf.ITextRenderer.setDocumentFromString(ITextRenderer.java:167)
	at org.xhtmlrenderer.pdf.ITextRenderer.setDocumentFromString(ITextRenderer.java:160)

@asolntsev
Copy link
Contributor

@andrzej-talarek Thank you for the feedback.
First of all, I assume that FS 9.3.1 didn't really load custom.css file, but just silently ignored it.
And FS 9.3.2 started searching this file in classpath and complaining if the file cannot be found.

Are you sure you have folder static in the classpath?

If yes, how can I reproduce the problem? Could you create a simple reproducible example?

@andrzej-talarek
Copy link

I'm 100% sure it worked fine - I've used several included styles (custom ones and bootstrap styles) and I know styles worked OK then (looking at final pdf).
Since it's spring-boot app, I'm not sure it would be that easy, but I can try to prepare a repo with as little code as it only requires.

@pbrant
Copy link
Member

pbrant commented Jan 3, 2024

@andrzej-talarek This should be fixed in #250.

@asolntsev
Copy link
Contributor

@andrzej-talarek The fix #250 has been released as FlyingSaucer 9.4.1
Can you please check if it helps?

@andrzej-talarek
Copy link

Confirmed, works perfectly again :-). Great thanks @asolntsev !

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

Successfully merging a pull request may close this issue.

4 participants