Html pdf 다운로드 - Html pdf daunlodeu

❮ HTML <a> tag

Example

Download file when clicking on the link (instead of navigating to the file):

<a href="/images/myw3schoolsimage.jpg" download>

Try it Yourself »

More "Try it Yourself" examples below.


Definition and Usage

The download attribute specifies that the target (the file specified in the href attribute) will be downloaded when a user clicks on the hyperlink.

The optional value of the download attribute will be the new name of the file after it is downloaded. There are no restrictions on allowed values, and the browser will automatically detect the correct file extension and add it to the file (.img, .pdf, .txt, .html, etc.).

If the value is omitted, the original filename is used.


Browser Support

The numbers in the table specify the first browser version that fully supports the attribute.

Attribute
download 14.0* 18.0 20.0* 10.1 15.0

* Chrome 65+ and Firefox only support same-origin download links.


Syntax

Attribute Values

ValueDescription
filename Optional. Specifies the new filename for the downloaded file

More Examples

Example

Specify a value for the download attribute, which will be the new filename of the downloaded file ("w3logo.jpg" instead of "myw3schoolsimage.jpg"):

<a href="/images/myw3schoolsimage.jpg" download="w3logo">

Try it Yourself »


❮ HTML <a> tag


내 웹 페이지에 다운로드 할 pdf 파일 링크를 아래와 같이 제공합니다.

<a href="myfile.pdf">Download Brochure</a>

문제는 사용자가이 링크를 클릭하면

  • 사용자가 Adobe Acrobat을 설치 한 경우 Adobe Reader의 동일한 브라우저 창에서 파일을 엽니 다.
  • Adobe Acrobat이 설치되어 있지 않으면 파일 다운로드를 위해 사용자에게 팝업됩니다.

하지만 “Adobe acrobat”의 설치 여부와 관계없이 항상 다운로드를 위해 사용자에게 팝업으로 표시되기를 바랍니다.

어떻게 할 수 있는지 알려주세요.



답변

.PDF 파일에 연결하는 대신 다음과 같은 작업을 수행하십시오.

<a href="pdf_server.php?file=pdffilename">Download my eBook</a>

사용자 정의 헤더를 출력하고 PDF (바이너리 세이프)를 열고 사용자의 브라우저에 데이터를 인쇄 한 다음 브라우저 설정에 관계없이 PDF를 저장하도록 선택할 수 있습니다. pdf_server.php는 다음과 같습니다.

header("Content-Type: application/octet-stream");

$file = $_GET["file"] .".pdf";
header("Content-Disposition: attachment; filename=" . urlencode($file));
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
header("Content-Length: " . filesize($file));
flush(); // this doesn't really matter.
$fp = fopen($file, "r");
while (!feof($fp))
{
    echo fread($fp, 65536);
    flush(); // this is essential for large downloads
}
fclose($fp); 

추신 : 그리고 분명히 “파일”변수에 대해 몇 가지 온 전성 검사를 실행하여 파일 확장명 허용 안 함, 슬래시 거부, 값에 .pdf 추가와 같은 파일을 훔치는 것을 방지합니다.


답변

이것은 일반적인 문제이지만 간단한 HTML 5 솔루션이 있다는 것을 아는 사람은 거의 없습니다.

<a href="./directory/yourfile.pdf" download="newfilename">Download the pdf</a>

newfilename사용자가 파일을 저장하기 위해 제안 된 파일 이름은 어디에 있습니까 ? 또는 다음과 같이 비워두면 서버 측의 파일 이름이 기본값으로 설정됩니다.

<a href="./directory/yourfile.pdf" download>Download the pdf</a>

호환성 : Firefox 21과 Iron에서 테스트했는데 둘 다 잘 작동했습니다. HTML5와 호환되지 않거나 오래된 브라우저에서는 작동하지 않을 수 있습니다. 다운로드를 강요하지 않은 유일한 브라우저는 IE입니다.

여기에서 호환성 확인 : http://caniuse.com/#feat=download


답변

모든 파일 행을 반복하지 마십시오 . 대신 readfile 을 사용하십시오 . 이것은 PHP 사이트에서 벗어났습니다 :
http://php.net/manual/en/function.readfile.php

$file = $_GET["file"];
if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header("Content-Type: application/force-download");
    header('Content-Disposition: attachment; filename=' . urlencode(basename($file)));
    // header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}

누군가가 일부 PHP 파일을 다운로드 할 수 있으므로 get 변수를 삭제하십시오.


답변

PHP 스크립트를 사용하는 대신 파일을 읽고 플러시하려면 .NET을 사용하여 헤더를 다시 작성하는 것이 더 깔끔합니다 .htaccess. 이렇게하면 “좋은”URL ( myfile.pdf대신 download.php?myfile) 이 유지됩니다 .

<FilesMatch "\.pdf$">
ForceType applicaton/octet-stream
Header set Content-Disposition attachment
</FilesMatch>


답변

우아하게 저하되는 평범한 HTML과 JavaScript / jQuery로 수행하는 방법을 찾았습니다. IE7-10, Safari, Chrome 및 FF에서 테스트되었습니다.

다운로드 링크 용 HTML :

<p>Thanks for downloading! If your download doesn't start shortly,
<a id="downloadLink" href="...yourpdf.pdf" target="_blank"
type="application/octet-stream" download="yourpdf.pdf">click here</a>.</p>

약간의 지연 후 링크 클릭을 시뮬레이션하는 jQuery (순수한 JavaScript 코드가 더 장황함) :

var delay = 3000;
window.setTimeout(function(){$('#downloadLink')[0].click();},delay);

이를 더 강력하게 만들기 위해 HTML5 기능 감지를 추가 할 수 있으며없는 경우 window.open()파일로 새 창을 여는 데 사용 합니다.


답변

이것이 핵심입니다.

header("Content-Type: application/octet-stream");

PDF 파일을 보내는 동안 Content-type application / x-pdf-document 또는 application / pdf가 전송됩니다. Adobe Reader는 일반적으로이 MIME 유형에 대한 핸들러를 설정하므로 PDF MIME 유형이 수신 될 때 브라우저가 문서를 Adobe Reader에 전달합니다.


답변

나는 이것에 대해 매우 늦었지만 자바 스크립트에서 이것을 할 수있는 해킹을 발견했습니다.

function downloadFile(src){
    var link=document.createElement('a');
    document.body.appendChild(link);
    link.href= src;
    link.download = '';
    link.click();
}