# IMPORTANT MODULES FOR THIS CODE... use CGI qw(-utf8); use File::Spec::Functions qw( catfile ); sub send_file { my ($cgi, $dir, $file) = @_; # $dir = '/var/www/download/'; # $file = 'MyLaTeXDocument.pdf'; my $path = catfile($dir, $file); open my $fh, '<:raw', $path or die "Cannot open '$path': $!\n"; $cgi->charset(''); #REMOVES PRIOR UTF-8 SETTING, AS THIS IS BINARY FILE print $cgi->header( -type => 'application/octet-stream', -attachment => $file, ); binmode STDOUT, ':raw'; print while <$fh>; close $fh or die "Cannot close '$path': $!"; return; } #### Connection Keep-Alive Content-Disposition attachment; filename="MyLaTeXDocument.pdf" Content-Type application/octet-stream Date Fri, 22 Sep 2023 11:13:27 GMT Keep-Alive timeout=5, max=100 Server Apache/2.4.52 (Ubuntu) Transfer-Encoding chunked #### JVBERi0xLjUKJeTw7fg... [truncated...too lazy to type more]