从外部URL读取PDF,并在触发按钮时自动下载

问题描述 投票:-2回答:1

我想从我的外部URL下载pdf,将其存储在我的服务器中,并在用户单击鼠标按钮触发按钮时下载。

我已经尝试将其存储到服务器并获得成功,但是我不知道如何在用户单击按钮后根据需要自动下载pdf。

我的看法:

<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>

我的控制器是:

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));
}

我想要的输出:

自动下载pdf

有什么办法吗?

php codeigniter pdf file-get-contents file-put-contents
1个回答
0
投票

您可以在PHP中使用file_put_contents()函数来读取远程文件并将其写入服务器目录。

$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));

更新

如果无法直接写文件,可以使用两步方法。首先创建空白文件,然后将其写入。

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

这将创建一个文件,以便其他功能可以写入该文件。

这样,您可以显示来自外部/内部URL的文件。并且在下载的情况下,您可以提供本地URL /documents/new_file.pdf

因此,当用户单击button时,您可以单击ajax,这会触发执行上述代码的服务器端脚本。

确保/documents/进程可写本地目录PHP

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

© www.soinside.com 2019 - 2024. All rights reserved.