Not sure what issue you're trying to solve.
If you want to preview the PDF with an option to download there's not really an easy way to do that. Maybe use a pop-up or modal to load the generated PDF inline (rather than as a downloadable attachment).
If your issue is that the browser doesn't display the PDF but instead just downloads it I don't think there's much else you can do. Your code is correct, but the actual handling of the PDF is controlled by the browser. If the browser doesn't have PDF viewing capability it will download the file.