最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - pdf download using php and onclick function - Stack Overflow

programmeradmin1浏览0评论

I have the following code.

     <div onClick="registerationform.pdf">something</div>
     <?php
     header('Content-disposition: attachment; filename=registerationform.pdf');
     header('Content-type: application/pdf');
     readfile('registerationform.pdf');

This code directly downloads the output if the page is loaded. But I need the pdf to get downloaded only if the something button is clicked.Help me

I have the following code.

     <div onClick="registerationform.pdf">something</div>
     <?php
     header('Content-disposition: attachment; filename=registerationform.pdf');
     header('Content-type: application/pdf');
     readfile('registerationform.pdf');

This code directly downloads the output if the page is loaded. But I need the pdf to get downloaded only if the something button is clicked.Help me

Share asked Nov 1, 2014 at 12:12 user4201875user4201875
Add a ment  | 

1 Answer 1

Reset to default 2

Php code is executed before any page content is shown or any javascript is executed, and not exactly sequentially as you see it in your example.

What you want is probably to create another php page downloadpdf.php which includes those headers you specified, and redirect the user to that page through a link:

link.php:

<a href="downloadpdf.php" target="_blank"> Download PDF </a>

Note: target="_blank" is added here so the actual page is not redirected but instead the new page is opened in a new tab-> the browser downloads the file and immediately closes the tab, "feeling" like it's an immediate download from the current page you are on.

downloadpdf.php

 <?php
 header('Content-disposition: attachment; filename=registerationform.pdf');
 header('Content-type: application/pdf');
 readfile('registerationform.pdf');
发布评论

评论列表(0)

  1. 暂无评论