-2

I would like to download a pdf from my external URL, store it in my server and download it when the button was triggered by click by the user.

I have tried to store it to my server and its success, but then I don't know how to automatically download the pdf as the user wants after they click the button.

My view:

<button id=\"appPrintbutton\" name=\"appPrintbutton\"  style=\"cursor:pointer;padding: 3px; margin: 2px;float:left;\" title=\"Cetak Dokumen\"><i class=\"fas  fa-check fa-lg fa-fw\"></i>Cetak</button>

<script type="text/javascript">
            $("#appPrintbutton").on('click', function() {
                    var selectedId=[];           
                    selectedId.push(document.getElementById("txtNO_DOK").value);            
                    $.ajax({
                        url: "<?php echo base_url().'index.php/finance/M_approve/DOWNLOAD_FILE_NILAI'?>",
                        type: 'POST',
                        data: {json: JSON.stringify(selectedId)},
                        dataType: 'json',
                        success: function (data) {
                            window.location.href = "<?php echo base_url().'index.php/finance/M_approve/';?>";
                          },
                        error: function (data) {
                            console.log('error');
                        }
                    });
                   return false;
               });
</script>

And my Controller was:

public function DOWNLOAD_FILE_NILAI()
{
    $msg="";
    $status="";
    $rptName="";
    $pathdir="/wwwroot/php/download/";
    $ieselon="ALL";
    $data=$this->input->post('json');
    $nodok=substr($data, 1, 9);
    if($nodok!="" )
    {
        $randfname = Date("Y_m_d");
        $fname = $nodok."_nilai".$randfname.".pdf";
        $rptName="\wfinance\kas_cetak.rpt";
        $strParamName= "&promptex-no_dok=".$nodok;
        $strParamName.= "&promptex-terbilang=".$nodok;
        $exportType="PDF"; 
        $serverLink = "http://11.5.1.44:12000/ReCrystallizeServer/ViewReport.aspx?report=".$rptName;
        $fullLink=$serverLink.$strParamName."&exportfmt=$exportType";
        $fdata = file_get_contents($fullLink);
        $fSaveAs=fopen($pathdir."$fname","w");
        fwrite($fSaveAs, $fdata);
        fclose($fSaveAs);       
        $status="OK";
    }
    $dataStatus[] = array(
       'Status'=>$status,
       'url'=>base_url(),
       'fname'=>$fname,
       'Msg'=>$msg
    );
    print_r(json_encode($dataStatus,JSON_PRETTY_PRINT));
}

My desired output:

Automatically download the pdf

Is it any way to do that?

baimWonk
  • 761
  • 1
  • 5
  • 11
  • What have you tried so far? What does all that code do, and what is not working yet? – Nico Haase Dec 06 '19 at 08:59
  • As you can see bro, I've been doing these all my code above. I'm confused to download it to a local directory, just because of it I am here – baimWonk Dec 06 '19 at 09:02

1 Answers1

0

You can use file_put_contents() function in PHP to read and write remote files to the server's directory.

$remote_file = 'http://example.com/path/to/pdffile.pdf';
$local_file = '/documents/new_file.pdf';
file_put_contents($local_file, file_get_contents($remote_file));

Update

If you are unable to write file directly, you can use two step way. First create blank file and after that write to it.

$local_file = fopen("/documents/new_file.pdf", "w")

This will create a file so other functions could write to it.

this way you can display files either from external/internal URL. And in case of downloading, you can provide local URL of /documents/new_file.pdf

So when the user clicks button you can hit ajax which triggers a server-side script which executes the code above.

Make sure your local directory /documents/ is writable by PHP process.

Read: https://www.php.net/manual/en/function.file-put-contents.php

Dheeraj Thedijje
  • 1,053
  • 12
  • 19
  • it doesn't work bro, give me an error file_put_contents(/documents/new_file.pdf): failed to open stream: No such file or directory – baimWonk Dec 06 '19 at 08:49
  • you have used two parameter right, and your directory is writable correct? – Dheeraj Thedijje Dec 06 '19 at 08:53
  • what directory should be writable you mean? is it my local document directory? – baimWonk Dec 06 '19 at 08:56
  • oh, you mean you want to download to local directory of the user not your server? – Dheeraj Thedijje Dec 06 '19 at 08:57
  • I've been practicing that on my script bro, $fSaveAs=fopen($pathdir."$fname","w"); – baimWonk Dec 06 '19 at 08:57
  • yes, coz download it into my server has been success. now I just want to download it into local directory when the button was triggered by user – baimWonk Dec 06 '19 at 08:59
  • in that case, you can use `$.get('url/to/file.pdf');` on clicking of the button, it will download the file to local directory of the user. for example try following in console. `$.get('https://source.android.com/security/reports/Google_Android_Security_2017_Report_Final.pdf');` make sure site have jquery in it. For other domain you might face cross origin error but not for your own domain. – Dheeraj Thedijje Dec 06 '19 at 09:01