in reply to Re: Perl output is not inducing file download as expected
in thread Perl output is not inducing file download as expected

One more thing that is still subtly wrong in your code is that you use the <> operator on a binary file, without changing the input separator $/. By using its default value (newline), you split your file on newlines and store its content in newline-separated chunks, which makes little to no sense for a PDF.

You could set it either to undef, which will cause the <> operator to slurp the entire file in one go, or you can set it to a reference to a suitable integer (e.g. local $/ = \32768;, which will cause it to read the file in fixed size chunks (this is useful when the don't want to, or can't read all of the content into memory).

See perldoc -v '$/' for a detailed explanation.

For a typical PDF setting it to undef and thus slurping the file would be the easiest, because then you wouldn't need the $len += length $_ for @document; dance to get your content length.