개발! 딱 깔끔하고 센스있게!

PHP + PDF, curl 다운로드하는 PDF 어떻게 사용합니까? 본문

개발 스크랩 메모/PHP

PHP + PDF, curl 다운로드하는 PDF 어떻게 사용합니까?

렉사이 2020. 12. 22. 22:42

어서 오세요.

내가 페이지에 다운로드한 pdf 파일을 저장할 때 문제가 있습니다.

pdf 를 다운로드하려면 Curl 사용하기:

$CurlConnect = curl_init(); curl_setopt($CurlConnect, CURLOPT_URL, 'http://website.com/invoices/download/1'); curl_setopt($CurlConnect, CURLOPT_POST, 1); curl_setopt($CurlConnect, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt($CurlConnect, CURLOPT_POSTFIELDS, $request); curl_setopt($CurlConnect, CURLOPT_USERPWD, $login.':'.$password); $Result = curl_exec($CurlConnect); 

현재 Result (string) 에서 모든 PDF 파일이 있습니다.

이제부터 나의 문제.다운로드를 저장하고 싶은 pdf:

header('Cache-Control: public'); header('Content-type: application/pdf'); header('Content-Disposition: attachment; filename="new.pdf"'); header('Content-Length: '.filesize($Result)); readfile($Result); 

불행한 것은 내가 저장하거나 새 PDF 파일을 열 때 공백문서를 얻을 수 있다는 것이다.

마지막 줄에 문제가 있을 수도 있습니다:

header('Content-Length: '.filesize($Result)); readfile($Result); 

불행하게도, 나는 그들을 어떻게 바꾸어야 할지 모르겠어.나는 너의 도움을 청한다.

감사합니다.

대답

filesize, readfile 모두 파일을 인자로 받습니다.

파일이 아닌 문자열을 제공합니다.

이것 좀 해 보세요.

$CurlConnect = curl_init(); curl_setopt($CurlConnect, CURLOPT_URL, 'http://website.com/invoices/download/1'); curl_setopt($CurlConnect, CURLOPT_POST, 1); curl_setopt($CurlConnect, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt($CurlConnect, CURLOPT_POSTFIELDS, $request); curl_setopt($CurlConnect, CURLOPT_USERPWD, $login.':'.$password); $Result = curl_exec($CurlConnect); header('Cache-Control: public'); header('Content-type: application/pdf'); header('Content-Disposition: attachment; filename="new.pdf"'); header('Content-Length: '.strlen($Result)); echo $Result; 
Comments