javaspringitextxhtmlrenderer

How to resolve constructor mismatch after updating xhtmlrenderer library?


I recently updated the xhtmlrenderer library in my Java project. After the update, I started getting an error related to the ITextUserAgent constructor in my FileResolvingUserAgent class. The error message is as follows:

error: constructor ITextUserAgent in class ITextUserAgent cannot be applied to given types;
    super(outputDevice);
    ^
required: ITextOutputDevice,int
found:    ITextOutputDevice
reason: actual and formal argument lists differ in length

Here is the code for my FileResolvingUserAgent class:

public FileResolvingUserAgent(final ITextOutputDevice outputDevice, final Map<String, Supplier<InputStream>> fileResolvers) {
    super(outputDevice);
    this.fileResolvers = fileResolvers;
}

And here is the constructor for ITextUserAgent:

public ITextUserAgent(ITextOutputDevice outputDevice, int dotsPerPixel) {
    super(Configuration.valueAsInt("xr.image.cache-capacity", 32));
    this._outputDevice = outputDevice;
    this.dotsPerPixel = dotsPerPixel;
}

It seems that the ITextUserAgent constructor now requires an additional int parameter dotsPerPixel, which wasn’t required before the update. I’m unsure what value I should pass for dotsPerPixel. Could anyone provide guidance on how to resolve this issue? Any help would be greatly appreciated. Thank you.

I tried to update the xhtmlrenderer library in my Java project. After the update, I attempted to run my project as usual.

I’m currently unsure about the appropriate value to pass for dotsPerPixel in the super(outputDevice, /* appropriate dotsPerPixel value */); line of my FileResolvingUserAgent class. I was expecting the xhtmlrenderer library update to be backward compatible, or at least have clear documentation on handling such changes. Any guidance on how to resolve this issue would be greatly appreciated. Thank you.


Solution

  • I had a similar issue to this, and resolved it by using the defaults from the ITextRenderer.class. I was already using this to render the PDF, so the defaults values match for our PDFs before and after the upgrade.

    So assuming you're using that as well, for you it might look something like:

    final ITextRenderer renderer = new ITextRenderer();
    ... 
    final var agent = new FileResolvingUserAgent(outputDevice, renderer.getSharedContext().getDotsPerPixel(), fileResolvers);
    

    And then a new constructor of:

    public FileResolvingUserAgent(final ITextOutputDevice outputDevice, final int dotsPerPixel, final Map<String, Supplier<InputStream>> fileResolvers) {
        super(outputDevice, dotsPerPixel);
        this.fileResolvers = fileResolvers;
    }
    

    Small note to be careful to use the dotsPerPixel and not the dotsPerPoint - although that shouldn't compile anyway as dotsPerPoint is a float.