StreamResource with dynamic filename

Dear community,
I am stuck with a Button that downloads dynamic context (PDF files) and would appreciate your help:

public class DownloadStreamButton extends Anchor {

    public DownloadStreamButton(String buttonText, String filename, Supplier<byte[]> bytes) {
        super(new StreamResource(filename, () -> new ByteArrayInputStream(bytes.get())), "");

        Button button = new Button(buttonText, VaadinIcon.CLOUD_DOWNLOAD.create());
        button.addThemeVariants(ButtonVariant.LUMO_SMALL);
        getElement().setAttribute("download", true);
        getElement().appendChild(button.getElement());
    }
}

The StreamResource accepts a Supplier for the data but not for the filename.

Any advice? Thank you!

You can take a look at https://vaadin.com/directory/component/flow-viritin where this any many other helpers are implemented.

Thank you very much @quirky-zebra .

        DynamicFileDownloader downloader = new DynamicFileDownloader(name, "",
                outputStream -> {
                    try {
                        outputStream.write(api.get());
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }) {

            @Override
            protected String getFileName(VaadinSession session, VaadinRequest request) {
                return "%s_%s_%d-%02d.pdf".formatted(name, employeeId.getValue(), yearSelect.getValue().getValue(), monthSelect.getValue().getValue());
            }
        };

I use https://vaadin.com/directory/component/lazy-download-button for that, works with Vaadin14 and Vaadin24