I’m using Vaadin 14.1.16 currently only for a few views on my page and include it using WebComponentExporter via JSP. I want to implement an Anchor that links to a Document using StreamResource:
Anchor aPrint = new Anchor();
aPrint.setText("Download Label PDF");
aPrint.setHref(createLabelStreamResource(file));
aPrint.getElement().setAttribute("download", true);
private StreamResource createLabelStreamResource(File file) {
StreamResource streamResource = new StreamResource("label.pdf", () -> {
try {
return new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return new ByteArrayInputStream("Something went wrong".getBytes()); // untested yet, not sure if that works
});
streamResource.setContentType("application/pdf");
return streamResource;
}
The Anchor is displayed on a website using something like the following URL:
http://localhost:8080/v/path/CATEGORY?entity=ENTITY_ID
The resulting Anchor element (using code see above) looks like this:
<a download="" href="VAADIN/dynamic/resource/1/bc2bc419-ecdd-4242-8fa2-37a4771dd581/label.pdf">Download PDF</a>
generally the href is correct, but when clicking on it it leads to
which includes the servlet path of the view where the anchor is displayed, whereas it actually should lead to
http://localhost:8080/VAADIN/dynamic/resource/1/bc2bc419-ecdd-4242-8fa2-37a4771dd581/label.pdf
So what I basically need is a leading slash for my href that it looks like
<a download="" href="/VAADIN/dynamic/resource/1/bc2bc419-ecdd-4242-8fa2-37a4771dd581/label.pdf">Download PDF</a>
and leads to the correct place. If I change it manually like this
aPrint.setHref("/" + aPrint.getHref());
the Anchor href will be correct but the dynamic content is detached and therefore deleted and not accessible.
Is there an elegant way to achieve that?