我正在使用php wkhtmltopdf从 html 创建一个 pdf
如果我将文件直接保存到我的服务器,则 pdf 有效
$pdf->saveAs('/path/to/mypdf.pdf');
如果我先将其转换为字符串,然后使用 php 将其保存到文件中,则 pdf 文件仍然有效。
$content = $pdf->toString();
$file = '/path/to/mypdf.pdf';
file_put_contents($file, $content);
但是无论我尝试包含什么标题,下载的pdf文件都不起作用,当我检查它时,它充满了�符号(据我所知,通常来自不正确的编码)
我已经尝试了所有我能想到的标头配置,但仍然得到相同的结果:
以下是我的一些尝试:
$response = $response->withHeader( 'Content-type', 'application/pdf' );
$content = $pdf->toString();
$response->write($content);
和
$response = $response->withHeader('Content-Type', 'application/pdf');
$response = $response->withHeader('Pragma', "public");
$response = $response->withHeader('Content-disposition', 'attachment; filename=test.pdf');
$response = $response->withHeader('Content-Transfer-Encoding', 'binary');
$response = $response->withHeader('Content-Length', strlen($content));
$response = $response->write($content);
return $response;
以下是下载前后 pdf 文件的内容示例:
下载前:
%PDF-1.4
1 0 obj
<<
/Title (˛ˇMyPDF Test type1)
/Creator (˛ˇwkhtmltopdf 0.12.3)
/Producer (˛ˇQt 4.8.7)
/CreationDate (D:20170312220641+02'00')
>>
endobj
3 0 obj
<<
/Type /ExtGState
/SA true
/SM 0.02
/ca 1.0
/CA 1.0
/AIS false
/SMask /None>>
endobj
4 0 obj
[/Pattern /DeviceRGB]
endobj
7 0 obj
<<
/Type /XObject
/Subtype /Image
/Width 301
/Height 181
/BitsPerComponent 8
/ColorSpace /DeviceGray
/Length 8 0 R
/Filter /FlateDecode
>>
stream
xúÌ]w@Gfl£É†R,®à
å
[DçöœäΩ˜D£±ã[å∆ª£bÕg"v
®{/ÿQ∞+E§fi|ª;≥w{ª≥ºF>˘˝°∑;˝«ÓÏõ˜fiº!,|ÇÍU∂!§·P¡ªÇgÖåú$,}ªœ€uÒ—ÎåÔ/Ôûfl∑í£ïºí•ÇÇ]%õ)·)’á*}~=r˜mFF∆ãQ·≠‹Kà’V=®Æµk÷®^›fløJï ï¸|+VÙˆ™‡QäPt)$"Ã$a˝ï1©ëdFn_ÕøA◊´ÜÚˆ‚&n2äñ9FÂæ]M"õÂ&∫7
3îìØ—ÅîΩ}*˝ΩkÄ4Nªÿ¨£ƒ9Ké"(ôŒπL2#·3‰:æπ+c|$◊áYJd´Ùê˛¸"ê\z¿CL˚o‘≤ƒf∑;#É,–ÿv3˝ˇ©2í£h¯úÓfiJ©|«>n/i™]2…™˙Ä U∏üÍ∑W†˝¸≈˛∏¸•œ ‡JŸ“.J.Y
YøäÁ≤
ª-fi‰ù‚Û£vd-∆&÷{ ‹˛ãŒòŒÁeêZñÿ≠W≤*˝ÆYJÚ£áI)ö˜¢™ã’†≤ü©⁄zÛ‘—cßn`5ˇ‹Ö_¬ı¢Ær˝K¸©O≤Bo©ÎV^Ÿ1(¥A`@˝–Å≥7_»Sß<j-RÖÓdπ›Ảò‹ õ|ém|€GD´¯zTñ_§‹e:)a√˙
ÇX€S·pPèd
»PÚ`EXvRŸNKÆ©sF
下载后:
PDF-1.4
1 0 obj
<<
/Title (���M�y�P�D�F� �T�e�s�t� �t�y�p�e�1)
/Creator (���w�k�h�t�m�l�t�o�p�d�f� �0�.�1�2�.�3)
/Producer (���Q�t� �4�.�8�.�7)
/CreationDate (D:20170312222109+02'00')
>>
endobj
3 0 obj
<<
/Type /ExtGState
/SA true
/SM 0.02
/ca 1.0
/CA 1.0
/AIS false
/SMask /None>>
endobj
4 0 obj
[/Pattern /DeviceRGB]
endobj
7 0 obj
<<
/Type /XObject
/Subtype /Image
/Width 301
/Height 181
/BitsPerComponent 8
/ColorSpace /DeviceGray
/Length 8 0 R
/Filter /FlateDecode
>>
stream
x��]w@Gߣ��R,��
�
[D��ϊ��D���[����b�g"v
�{/�Q�+E��|�;�w{���F>����;�����!,|��U�!��P���g���$,}���u����/
�߷��������]%�)�)��*}~=r�mFFƋQ��K��V=���k�
n2��9F�]M"��&�7
3���с��}*��k�4N�ج��9K�"(�ιL2#�3�:��+c|$ׇYJd�����"�\z�CL�oԲ�f�;#�,��v3���2��h����J�|�>n/i�]2ɪ���U���W�����������J��.J.Y
Y���
�-����vd-�&�{ ���Θ��e�Z�حW�*��YJ�I)�����ՠ����z���c�n`5�܅_����r�K��O�Bo��V^�1(�A`@�Ё�7_�S�<j-R��d��A����ʛ|�m|�GD��zT�_��e:)a��