PHP 中的 Base64 到 PDF - 文件损坏
Base64 to PDF in PHP - Getting corrupted file
我正在尝试使用 PHP 从 Base64 字符串生成 PDF。
当我将 base64 粘贴到浏览器或使用站点 base64.guru 时,base64 可以正常工作,但是每次它在本地写入并且当我尝试打开它时,它都说它已损坏。
Base64 字符串是
$text = 'data:application/pdf;base64,JVBERi0xLjMKMSAwIG9iago8PCAvVHlwZSAvQ2F0YWxvZwovT3V0bGluZXMgMiAwIFIKL1BhZ2VzIDMgMCBSID4+CmVuZG9iagoyIDAgb2JqCjw8IC9UeXBlIC9PdXRsaW5lcyAvQ291bnQgMCA+PgplbmRvYmoKMyAwIG9iago8PCAvVHlwZSAvUGFnZXMKL0tpZHMgWzYgMCBSCl0KL0NvdW50IDEKL1Jlc291cmNlcyA8PAovUHJvY1NldCA0IDAgUgovRm9udCA8PCAKL0YxIDggMCBSCi9GMiA5IDAgUgovRjMgMTAgMCBSCi9GNCAxMSAwIFIKPj4KL0V4dEdTdGF0ZSA8PCAKL0dTMSAxMiAwIFIKL0dTMiAxMyAwIFIKL0dTMyAxNCAwIFIKL0dTNCAxNSAwIFIKPj4KPj4KL01lZGlhQm94IFswLjAwMCAwLjAwMCA1OTUuMjgwIDg0MS44OTBdCiA+PgplbmRvYmoKNCAwIG9iagpbL1BERiAvVGV4dCBdCmVuZG9iago1IDAgb2JqCjw8Ci9Qcm9kdWNlciAo/v8AZABvAG0AcABkAGYAIAArACAAQwBQAEQARikKL0NyZWF0aW9uRGF0ZSAoRDoyMDE5MDgyMjE2NDI1NyswMCcwMCcpCi9Nb2REYXRlIChEOjIwMTkwODIyMTY0MjU3KzAwJzAwJykKPj4KZW5kb2JqCjYgMCBvYmoKPDwgL1R5cGUgL1BhZ2UKL01lZGlhQm94IFswLjAwMCAwLjAwMCA1OTUuMjgwIDg0MS44OTBdCi9QYXJlbnQgMyAwIFIKL0NvbnRlbnRzIDcgMCBSCj4+CmVuZG9iago3IDAgb2JqCjw8IC9GaWx0ZXIgL0ZsYXR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5ZWIxNDIyYTgyZGU2N2ZmNDdiYj48MjlhOWIyOTQyNjFkOWViMTQyMmE4MmRlNjdmZjQ3YmI+XQo+PgpzdGFydHhyZWYKMTg0OQolJUVPRgo=';
这是我尝试使用的。它将文件写入文件夹,但它说它已损坏。我已经尝试了所有关于堆栈溢出的不同解决方案,但似乎无法使其工作。
$data = sanitize_text_field(base64_encode($text));
file_put_contents('output.pdf', base64_decode($data));
尝试删除 "data:application/pdf;base64,"
我正在尝试使用 PHP 从 Base64 字符串生成 PDF。 当我将 base64 粘贴到浏览器或使用站点 base64.guru 时,base64 可以正常工作,但是每次它在本地写入并且当我尝试打开它时,它都说它已损坏。
Base64 字符串是
$text = 'data:application/pdf;base64,JVBERi0xLjMKMSAwIG9iago8PCAvVHlwZSAvQ2F0YWxvZwovT3V0bGluZXMgMiAwIFIKL1BhZ2VzIDMgMCBSID4+CmVuZG9iagoyIDAgb2JqCjw8IC9UeXBlIC9PdXRsaW5lcyAvQ291bnQgMCA+PgplbmRvYmoKMyAwIG9iago8PCAvVHlwZSAvUGFnZXMKL0tpZHMgWzYgMCBSCl0KL0NvdW50IDEKL1Jlc291cmNlcyA8PAovUHJvY1NldCA0IDAgUgovRm9udCA8PCAKL0YxIDggMCBSCi9GMiA5IDAgUgovRjMgMTAgMCBSCi9GNCAxMSAwIFIKPj4KL0V4dEdTdGF0ZSA8PCAKL0dTMSAxMiAwIFIKL0dTMiAxMyAwIFIKL0dTMyAxNCAwIFIKL0dTNCAxNSAwIFIKPj4KPj4KL01lZGlhQm94IFswLjAwMCAwLjAwMCA1OTUuMjgwIDg0MS44OTBdCiA+PgplbmRvYmoKNCAwIG9iagpbL1BERiAvVGV4dCBdCmVuZG9iago1IDAgb2JqCjw8Ci9Qcm9kdWNlciAo/v8AZABvAG0AcABkAGYAIAArACAAQwBQAEQARikKL0NyZWF0aW9uRGF0ZSAoRDoyMDE5MDgyMjE2NDI1NyswMCcwMCcpCi9Nb2REYXRlIChEOjIwMTkwODIyMTY0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DAwMCBuIAowMDAwMDAxNjE1IDAwMDAwIG4gCjAwMDAwMDE2NzUgMDAwMDAgbiAKMDAwMDAwMTczNSAwMDAwMCBuIAowMDAwMDAxNzkyIDAwMDAwIG4gCnRyYWlsZXIKPDwKL1NpemUgMTYKL1Jvb3QgMSAwIFIKL0luZm8gNSAwIFIKL0lEWzwyOWE5YjI5NDI2MWQ5ZWIxNDIyYTgyZGU2N2ZmNDdiYj48MjlhOWIyOTQyNjFkOWViMTQyMmE4MmRlNjdmZjQ3YmI+XQo+PgpzdGFydHhyZWYKMTg0OQolJUVPRgo=';
这是我尝试使用的。它将文件写入文件夹,但它说它已损坏。我已经尝试了所有关于堆栈溢出的不同解决方案,但似乎无法使其工作。
$data = sanitize_text_field(base64_encode($text));
file_put_contents('output.pdf', base64_decode($data));
尝试删除 "data:application/pdf;base64,"