Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript automatic pdf download

  • This topic is empty.
Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #249874
    chauhanheena
    Participant

    Hi

    the page in concern in
    http://www.cubegrfx.com/projects/vnc/bestbuild/wallputtyPDF.html

    After the form is submitted and the success message shows up, a pdf download should automatically start. how to achieve this.

    Thanks!

    #249879
    Shikkediel
    Participant

    If you want to do it with JS, I think you could create a link to the file and then trigger a click on it. But it’s probably more straightforward to serve the file with PHP.

    #249883
    chauhanheena
    Participant

    below is the php code. What code should i add to this for the automatic download to start.


    <?php // CHANGE EMAIL ADDRESS ON LINE 45. if(!$_POST) exit; // Email address verification, do not edit. function isEmail($email) { return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email)); } if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n"); $spurce = $_POST['source'] $cname = $_POST['cname']; $product = $_POST['product']; $phone = $_POST['phone']; $mobile = $_POST['mobile']; $fname = $_POST['fname']; $lname = $_POST['lname']; $email = $_POST['email']; $remarks = $_POST['remarks']; $verify = $_POST['verify']; if(trim($fname) == '') { echo '<div class="error_message">You must enter your first name.</div>'; exit(); } else if(trim($lname) == '') { echo '<div class="error_message">You must enter your last name.</div>'; exit(); } else if(trim($cname) == '') { echo '<div class="error_message">You must enter your company name.</div>'; exit(); } else if(trim($phone) == '') { echo '<div class="error_message">You must enter your phone no.</div>'; exit(); } else if(trim($mobile) == '') { echo '<div class="error_message">You must enter your mobile no.</div>'; exit(); } else if(trim($email) == '') { echo '<div class="error_message">Please enter a valid email address.</div>'; exit(); } else if(!isEmail($email)) { echo '<div class="error_message">You have enter an invalid e-mail address, try again.</div>'; exit(); } if(get_magic_quotes_gpc()) { $remarks = stripslashes(remarks); } // Configuration option. // Enter the email address that you want to emails to be sent to. //$address = "[email protected]"; $address = "[email protected]"; // Configuration option. // i.e. The standard subject will appear as, "You've been contacted by John Doe." // Example, $e_subject = '$name . ' has contacted you via Your Website.'; $e_subject = $cname . '/' . $fname . ' ' . $lname . ' ' . $source . ' '; // Configuration option. // You can change this if you feel that you need to. // Developers, you may wish to add more fields to the form, in which case you must be sure to add them here. $e_source = "Source: $source" . PHP_EOL . PHP_EOL; $e_cname = "Custome Name: $cname" . PHP_EOL . PHP_EOL; $e_product = "Product: $product" . PHP_EOL . PHP_EOL; $e_phone = "Phone No.: $phone" . PHP_EOL . PHP_EOL; $e_mobile = "Mobile: $mobile" . PHP_EOL . PHP_EOL; $e_name = "Contact Person: $fname $lname" . PHP_EOL . PHP_EOL; $e_email = "Email: $email" . PHP_EOL . PHP_EOL; $e_remarks = "Remarks: $remarks"; $msg = $e_source . $e_cname . $e_product . $e_phone . $e_mobile . $e_name . $e_email . $e_remarks; $headers = "From: $email" . PHP_EOL; $headers .= "Reply-To: $email" . PHP_EOL; $headers .= "MIME-Version: 1.0" . PHP_EOL; $headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL; $headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL; if(mail($address, $e_subject, $msg, $headers)) { // Email has sent successfully, echo a success page. echo "<fieldset>"; echo "<div id='success_page'>"; echo "<h4 class='highlight'>Thank you! <strong>$fname</strong>PDF download will automatically start now.</h4>"; echo "</div>"; echo "</fieldset>"; } else { echo 'ERROR!'; }
    #249884
    Beverleyh
    Participant

    I think you’re looking for readfile() http://php.net/manual/en/function.readfile.php

    Otherwise, try Googling something like “php header force download PDF”.

    #249928
    Shikkediel
    Participant

    This one’s handy to force a download dialogue and not open the file:

    https://css-tricks.com/snippets/htaccess/force-files-to-download-not-open-in-browser/

    Beverley’s posted the exact terms to find the solution but let me just add what I’ve been using:

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="anyname.pdf"');
    header('Expires: 0');
    header('Cache-Control: no-cache, no-store, must-revalidate');
    header('Pragma: no-cache');
    
    readfile('path/correctname.pdf');
    
    #250166
    chauhanheena
    Participant

    Hi
    I used the below code

    <?php
    if(!$_POST) exit;
    
    function isEmail($email) {
        return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
    }
    
    if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
    
    $source = $_POST['source'];
    $cname   = $_POST['cname']; 
    $product = $_POST['product'];
    $phone = $_POST['phone'];
    $mobile = $_POST['mobile'];
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $email = $_POST['email'];
    $remarks = $_POST['remarks'];
    $verify   = $_POST['verify'];
    
    if(trim($fname) == '') {
        echo '<div class="error_message">You must enter your first name.</div>';
        exit();
    } else if(trim($lname) == '') {
        echo '<div class="error_message">You must enter your last name.</div>';
        exit();
    } else if(trim($cname) == '') {
        echo '<div class="error_message">You must enter your company name.</div>';
        exit();
    } else if(trim($phone) == '') {
        echo '<div class="error_message">You must enter your phone no.</div>';
        exit();
    } else if(trim($mobile) == '') {
        echo '<div class="error_message">You must enter your mobile no.</div>';
        exit();
    } else if(trim($email) == '') {
        echo '<div class="error_message">Please enter a valid email address.</div>';
        exit();
    } else if(!isEmail($email)) {
        echo '<div class="error_message">You have enter an invalid e-mail address, try again.</div>';
        exit();
    }
    
    if(get_magic_quotes_gpc()) {
        $remarks = stripslashes(remarks);
    }
    
    $address = "[email protected]";
    
    $e_subject = $cname . '/' . $fname . ' ' . $lname . ' ' . $source . ' ';
    
    $e_source = "Source: $source" . PHP_EOL . PHP_EOL;
    $e_cname = "Custome Name: $cname" . PHP_EOL . PHP_EOL;
    $e_product = "Product: $product" . PHP_EOL . PHP_EOL;
    $e_phone = "Phone No.: $phone" . PHP_EOL . PHP_EOL;
    $e_mobile = "Mobile: $mobile" . PHP_EOL . PHP_EOL;
    $e_name = "Contact Person: $fname $lname" . PHP_EOL . PHP_EOL;
    $e_email = "Email: $email" . PHP_EOL . PHP_EOL;
    $e_remarks = "Remarks: $remarks";
    
    $msg = $e_source . $e_cname . $e_product . $e_phone . $e_mobile . $e_name . $e_email . $e_remarks;
    
    $headers = "From: $email" . PHP_EOL;
    $headers .= "Reply-To: $email" . PHP_EOL;
    $headers .= "MIME-Version: 1.0" . PHP_EOL;
    $headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
    $headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
    
    if(mail($address, $e_subject, $msg, $headers)) {
    
        // Email has sent successfully, echo a success page.
    
        echo "<fieldset>";
        echo "<div id='success_page'>";
        echo "<h4 class='highlight'>Thank you! <strong>$fname</strong>. PDF download will automatically start now.</h4>";
        echo "</div>";
        echo "</fieldset>";
    
    
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="BESTBUILD-Wall-Putty-FN.pdf"');
    header('Expires: 0');
    header('Cache-Control: no-cache, no-store, must-revalidate');
    header('Pragma: no-cache');
    readfile('BESTBUILD-Wall-Putty-FN.pdf');
    
    } else {
        echo 'ERROR!';
    }
    
    
    

    but the result is, an mail is sent and on the success page the following is shown.

    Thank you! Heena. PDF download will automatically start now.

    %PDF-1.5 %���� 73 0 obj << /Linearized 1 /L 1575021 /H [ 1901 415 ] /O 75 /E 1528187 /N 3 /T 1573434 >> endobj xref 73 58 0000000017 00000 n 0000001630 00000 n 0000002316 00000 n 0000002881 00000 n 0000002952 00000 n 0000003026 00000 n 0000003134 00000 n 0000003200 00000 n 0000003630 00000 n 0000004109 00000 n 0000036414 00000 n 0000036454 00000 n 0000037290 00000 n 0000037364 00000 n 0000037444 00000 n 0000102395 00000 n 0000134964 00000 n 0000138623 00000 n 0000138785 00000 n 0000138825 00000 n 0001504275 00000 n 0001504388 00000 n 0001504492 00000 n 0001504654 00000 n 0001504810 00000 n 0001504938 00000 n 0001505038 00000 n 0001505140 00000 n 0001505300 00000 n 0001505458 00000 n 0001505621 00000 n 0001505736 00000 n 0001505845 00000 n 0001506008 00000 n 0001506123 00000 n 0001506232 00000 n 0001506394 00000 n 0001506557 00000 n 0001506731 00000 n 0001507340 00000 n 0001507509 00000 n 0001507592 00000 n 0001507877 00000 n 0001512895 00000 n 0001513272 00000 n 0001513435 00000 n 0001513789 00000 n 0001513947 00000 n 0001514221 00000 n 0001514963 00000 n 0001515068 00000 n 0001515230 00000 n 0001515563 00000 n 0001515720 00000 n 0001515996 00000 n 0001516508 00000 n 0001516565 00000 n 0000001901 00000 n trailer << /Size 131 /Prev 1573423 /Info 72 0 R /Root 74 0 R /ID [] >> startxref 0 %%EOF 74 0 obj << /Type /Catalog /Pages 62 0 R /Names << /Dests 67 0 R >> /Outlines 63 0 R /OCProperties << /D << /Order [ 86 0 R 59 0 R 60 0 R 61 0 R ] /ON [ 86 0 R 59 0 R 60 0 R 61 0 R ] /OFF [ ] /RBGroups [ ] >> /OCGs [ 86 0 R 59 0 R 60 0 R 61 0 R ] >> >> endobj 130 0 obj << /S 155 /O 367 /Filter /FlateDecode /Length 316 >> stream x���M(Dq����|gF�i�,�XZ�(䩷��d��xYMIi��ެ���D�lX%��d�j�Ӱ�d!I����X�u���{���\�8��$�o�$�Bý����$2��x,��+i�ƅE�ͻ~0|�,?FNg����Vŭo��������R,�=�3Uw8��.M4��b����/Y�����)��^�HY� \��5�*��t\�<�]�BR�T!�a�3�yp�’,V� zE|Ѓx” ���Y����JQ�2nSD,E�a�J.˨.��ݨ{����й�o��]��͖(�ɒD\��w��J#R�_{}����KM��<�O� endstream endobj 75 0 obj << /Type /Page /CropBox [ 0 0 595.276 822.047 ] /MediaBox [ 0 0 595.276 822.047 ] /Resources << /ExtGState << /GS11 76 0 R /GS13 77 0 R /GS71 78 0 R >> /Properties << /Pr12 86 0 R >> /XObject << /X0

    #250196
    Shikkediel
    Participant

    I’m not great at PHP myself… so I thought leaving out a few lines that didn’t seem very relevant wouldn’t make much of a difference. But this is the exact code I’m using (mostly a copy and paste from quite a while back):

    ...
    
    ob_clean();
    flush();
    readfile('path/correctname.pdf');
    exit();
    
    #250856
    chauhanheena
    Participant

    Hi @shikkediel

    I tried the above too. but don’t work and looks like I’m no were near getting a solution to this.

    I finally tried this but till not there.

    in the sendmail.php , I/m trying to call another php file (download.php) which starts the download.

    if(mail($address, $e_subject, $msg, $headers)) {
        header( "Refresh:5; url=download.php");
        echo "<fieldset>";
        echo "
    "; echo "Thank you! $fname. PDF download will automatically start right now."; echo "
    "; echo "</fieldset>"; } else { echo 'ERROR!'; }

    download.php

    <?php
    header("Content-type:application/pdf");
    
    // Use the name of the pdf file that is saved as… Can be different from the file on the server
    header("Content-Disposition:attachment;filename=sample.pdf");
    
    // The PDF file name on the erver
    readfile("sample.pdf");
    ?>
    
    

    when i run the download.php int eh browser, the pdf downloads, but via sendmail.php, does not. anyhow the mail is sent.
    any suggestions on this

    thanks!

    #250859
    Shikkediel
    Participant

    Not my strong suit… but have you tried an include for the download.php instead of a header? Let me holler @atelierbram and @beverleyh though without trying to impose, both undoubtedly a lot more experienced at this sort of thing than me.

    #250872
    Beverleyh
    Participant

    Holler heard! ;)

    I’m guessing that what may be causing issues is that you’re echoing HTML output before your header download code, and you can’t modify header information when headers have already been sent. If you turn PHP error reporting on, I suspect you’ll see errors outputted in the browser stating something along those lines.

    So, what you should do is reorder your code so that the headers come before any HTML output (any of those echos, or other HTML that may be sat higher up the PHP file outside of PHP tags – including doctype), which, depending on your page, might be an easy refactoring job or it might be more complex.

    On the other hand, you could try using ob_start(); at the very top of your PHP page. It isn’t ideal (and I’m likely to get stomped on for suggesting it), but its a workaround that may get you the results you’re after if you’re really in a bind.

    But, turn error reporting on to check for the actual errors – they’re going to be the best indicators to help you troubleshoot.

    #250890
    Shikkediel
    Participant

    Thanks for jumping in. I might learn a thing or two myself (which is quite necessary).

Viewing 11 posts - 1 through 11 (of 11 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.