danfickle / openhtmltopdf

An HTML to PDF library for the JVM. Based on Flying Saucer and Apache PDF-BOX 2. With SVG image support. Now also with accessible PDF support (WCAG, Section 508, PDF/UA)!
https://danfickle.github.io/pdf-templates/index.html
Other
1.9k stars 355 forks source link

Add a way to resolve Images/Css locally #91

Open schmitch opened 7 years ago

schmitch commented 7 years ago

Currently it would be great if there was a way to have a CssProvider and a ImageProvider, that can actually resolve images and fonts.

Consider a HTML with multiple Images that have relative paths like: images/my_image.png it would be amazing if one could resolve them locally without making a URL request, like it's done in iText via:

new AbstractImageProvider() {
    @Override
    public String getImageRootPath() {
        return environmentPath("/my_path");
     }

    @Override
     public Image retrieve(String src) {
        if (src.startsWith("/barcode/delivery/")) {
            String type = src.substring(18);
            return barcode((DeliveryRequest) data, writer, type);
        } else {
            return super.retrieve(src);
        }
    }
}

this would also give the possibility to create a image dynamically.

Same goes for CSS. (Currently the only way is to override the httpStreamImplementation, which works for Images only I guess. And a CSS should be providable without being inside the html)

ieugen commented 7 years ago

Hi @schmitch ,

Annother use case would be to resolve the resources in memory, not necessary on disk. We use wkhtmltopdf and generate custom CSS per report. With wkhtmltopdf we need to write it to disk. Since it is a temporary file, it will be removed after the report is generated.

Having the ability to plug in resolvers, we might provide resources via another path like InputStreams and such.

danfickle commented 7 years ago

Hello, With the pluggable http stream factory, you can already do that for http/https urls. Possibly, you could also abuse the pluggable cache to provide Image objects. See integration guide. However, it has become apparent from this thread that you should be able to do it with all url protocols and that we need a better API.