Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过导航到codeigniter中的某个位置下载创建的pdf_Javascript_Html_Codeigniter_Pdf - Fatal编程技术网

Javascript 通过导航到codeigniter中的某个位置下载创建的pdf

Javascript 通过导航到codeigniter中的某个位置下载创建的pdf,javascript,html,codeigniter,pdf,Javascript,Html,Codeigniter,Pdf,我使用DOMpdf创建了一个页面的pdf文件,如下所示 <?php use Dompdf\Dompdf; class Pdfgenerator { public function generate($html, $filename='', $stream=TRUE, $paper = 'A4', $orientation = "portrait") { $dompdf = new DOMPDF(array('enable_remote' => true));

我使用
DOMpdf
创建了一个页面的pdf文件,如下所示

<?php

use Dompdf\Dompdf;
class Pdfgenerator {

  public function generate($html, $filename='', $stream=TRUE, $paper = 'A4', $orientation = "portrait")
  {
    $dompdf = new DOMPDF(array('enable_remote' => true));
    $dompdf->loadHtml($html);
    $dompdf->setPaper($paper, $orientation);
    $dompdf->render();
    if ($stream) {
        $dompdf->stream($filename.".pdf", array("Attachment" => 0));
    } else {
        return $dompdf->output();
    }
  }
}
我真正想要的是在给定的路径上下载文件,这样当创建PDF时,将打开一个已导航到给定位置的保存提示。如果是正确的位置,我可以按save,如果不是,我可以导航到正确的保存位置。 这些都是我已经问过的一些类似的问题,但我觉得它们没有帮助


您可以在php中创建如下脚本,将生成的pdf发送到浏览器

 <?php


use Dompdf\Dompdf;
class Pdfgenerator {

  public function downLoadPdf(){
        $name= "testfile.pdf";

        header('Content-Description: File Transfer');
        header('Content-Type: application/force-download');
        header("Content-Disposition: attachment; filename=\"" . basename($name) . "\";");
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($name));
        ob_clean();
        flush();
        readfile("your_file_path/".$name); //showing the path to the server where the file is to be download

    }
}
    ?>

只需在类中添加此函数,并在单击“保存”按钮时使用ajax调用它(如果它存在于视图页面中)

当您单击“保存”按钮从浏览器调用此脚本时,浏览器将自动打开“保存”窗口,您可以将文件保存到所需位置

这似乎不起作用。我在创建pdf后添加了这段代码。i、 e在
$this->pdfgenerator->generate($html,$filename,true,'a4','')
之后,问题是相同的,pdf将在浏览器页面中打开,没有显示保存窗口。我已编辑了答案。您应该创建一个新函数,并在视图中的“保存”按钮上调用它。我感谢您的帮助,但我无法理解如何使用您的代码段生成pdf,即,在“保存”按钮调用时,调用将html转换为pdf的函数(即,
$this->pdfgenerator->generate($html,$filename,true,'a4','')
),如果我在保存时调用downloadPdf,
testfile.pdf
甚至还不会被创建来下载??当你调用
generate
函数时,你必须将pdf保存在某个地方。我在回答中添加的代码片段不会生成pdf,它会将保存的pdf从你的服务器位置发送到浏览器。你的函数只是生成pdf文件和d我把它放在浏览器里。你必须把它保存在服务器的某个地方以便下载。希望你明白我的意思
 <?php


use Dompdf\Dompdf;
class Pdfgenerator {

  public function downLoadPdf(){
        $name= "testfile.pdf";

        header('Content-Description: File Transfer');
        header('Content-Type: application/force-download');
        header("Content-Disposition: attachment; filename=\"" . basename($name) . "\";");
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($name));
        ob_clean();
        flush();
        readfile("your_file_path/".$name); //showing the path to the server where the file is to be download

    }
}
    ?>