barryvdh / laravel-snappy

Laravel Snappy PDF
MIT License
2.59k stars 289 forks source link

Incorrect File Name Encoding on Safari When Downloading PDF with Non-ASCII Characters #517

Open saiko-daidv opened 9 months ago

saiko-daidv commented 9 months ago

I am currently using laravel-snappy to generate PDF files in a Laravel application. The library works as expected on browsers like Chrome, but I encountered an issue when downloading files using Safari. Specifically, when the filename contains non-ASCII characters (e.g., Japanese characters), the name gets incorrectly encoded.

For example, a correct filename should appear as follows: #335テスト_社内見積_20231001.pdf

However, in Safari, it is incorrectly displayed as: #335ãã¹ã_社åè¦ç©_20231001.pdf

Here's a snippet of the code I'm using:

public function projectDetailPdf($id, Request $request) {
    $project = $this->projectRepository->find($id);
    $pdf = \PDF::loadView('pdf.project_details', [
        'project' => $project,
    ])
    ->setOption('encoding', 'utf-8')
        ->setPaper('a4', 'landscape');

    $filename = $project['name'] . '_プロジェクト詳細_' . date("Ymd") . '.pdf';

    $headers = [
        'Content-Type'        => 'application/pdf',
        'Content-Disposition' => 'attachment; filename*=UTF-8\'\''.rawurlencode($filename)
    ];

    return $pdf->stream($filename, 200, $headers);
}

I tried using both rawurlencode and urlencode for the filename in the Content-Disposition header, but neither resolved the issue.

Laravel version: ^6.20.26 laravel-snappy version: ^0.4.8 PHP version: 7.4

Please help me! Thank you!