Question

With Wicket 1.5, I need to display a PDF file in a new Window. To do that, I extend ByteArrayResource. The problem is that there is only two options in content disposition: attachment and inline.

The first one download the file, and it's too long for my users to do that when they want to just consult the file. The second one open the file in the same window and the users want a new window to continue to work with the document opened.

Here is my ByteArrayResource code:

public class FileArchiveRessource extends ByteArrayResource {

    private final String genericName;
    private final Locale locale;

    public FileArchiveRessource(String genericName, Locale locale) {
        super("application/pdf");
        this.genericName = genericName;
        this.locale = locale;
        this.contentType = "application/pdf";
    }

    @Override
    protected byte[] getData(IResource.Attributes attributes) {
        try {
            String name = genericName.replaceAll("[{][$][}]", locale.getLanguage().toUpperCase());
            return IOUtils.toByteArray(this.getClass().getResourceAsStream(name));
        } catch (IOException ex) {
            return new byte[0];
        }
    }

    @Override
    protected void configureResponse(ResourceResponse response, Attributes attributes) {
        super.configureResponse(response, attributes);
        response.setContentDisposition(ContentDisposition.INLINE);
    }
}

In this implementation, {$} in the name is replaced by the language of the user.

How the resource is instanciated:

final Locale locale = getLocale();
final String name = "document-{$}.pdf".replaceAll("[{][$][}]", locale.getLanguage().toUpperCase());
ResourceLink button = new ResourceLink("list.openbutton", new ResourceReference(name) {
    @Override
    public IResource getResource() {
        return new FileArchiveRessource(name, locale);
    }
});

add(button);

And here is an example of a open button of my resource:

<input wicket:id="list.openbutton"
       type="submit"
       class="button openbutton"
       value="Open" title="Open resource" />

Any suggestion?

Precision: I really need a new separate window, not a new tab.

Was it helpful?

Solution 2

Thanks to Joachim, I found a solution that fit exactly to my needs. I use javascript to open a new window with the document in it.

Here is the javascript:

function openPDF(source, name) {
    var screenHeight = screen.height;
    var height = screenHeight - 100;
    var width = height / (1.3);

    var specs = 'directories=0, location=0, toolbar=0, menubar=0, resizable=1, status=0';
    specs += ', height=' + height;
    specs += ', width=' + width;
    specs += ', left=' + (screen.width - width)/2;
    specs += ', top=' + 10;

    window.open(source, name, specs);
    return false;
}

And the button:

<a href="#" target="_blank" wicket:id="list.openbutton"
        class="button openbutton" onClick="return openPDF(this.href, 'PDF file')">Open</a>

OTHER TIPS

Why are you not just using a simple link instead of a button in your HTML?

<a href="#" target="_blank" wicket:id="list.openbutton">Open resource</a>

Try adding onclick="target='_blank';return true;" to your input

I think the best solution is using this: This will open a new window with popup configuration.

PopupSettings popupSettings = new PopupSettings(PopupSettings.RESIZABLE | PopupSettings.SCROLLBARS).setHeight(500).setWidth(700);

ResourceLink button = new ResourceLink("list.openbutton", new ResourceReference(name) {
    @Override
    public IResource getResource() {
        return new FileArchiveRessource(name, locale);
    }
});

button.setPopupSettings(popupSettings);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top