如何降低高度并使用FPDF下载pdf?

时间:2017-01-13 15:08:35

标签: php laravel

我有一个在laravel中使用 Fpdf 生成pdf的功能。 我的问题是:

  1. 毕竟Cell我有一些额外的空间。我需要删除它。请找到下面的图片。
  2. 如何将此pdf文件下载到我的系统中。目前它只是显示在浏览器中。代码示例如下。
  3. 代码

    Controller:Controller.php

    public function index()
        {
            $orders = Order::select('firstname', 'lastname', 'street', 'postal', 'country')->get();
            foreach ($orders as $order){
                Fpdf::SetMargins(5, 5, 5);
                Fpdf::AddPage('L', array(60,90), 'A4');
                Fpdf::SetAutoPageBreak(TRUE, 0);
                Fpdf::SetFont('helvetica', '', 7); //IF bold letter SetFont('Arial','B',14)
                Fpdf::SetTextColor(0, 0, 0);
                Fpdf::Cell(10,5,iconv('UTF-8', 'windows-1252', 'Falls unzustellbar, zurück an Absender'),0,"1","L");
                Fpdf::SetFont('','U');
                Fpdf::Cell(10,5,iconv('UTF-8', 'windows-1252', 'schrillALARM.jetzt c/o 365group • Grasgasse 2 • 93047 Regensburg'),0,"1","L");
                Fpdf::SetFont('helvetica', '', 11);
                Fpdf::Cell(10,5,$order->firstname,0,1,"L");
                Fpdf::Cell(10,5,$order->lastname,0,1,"L");
                Fpdf::Cell(10,5,$order->street,0,1,"L");
                Fpdf::Cell(10,5,$order->postal,0,1,"L");
                Fpdf::Cell(10,5,$order->country,0,1,"L");
            }
            Fpdf::Output();
            exit;
        }
    

    路线:路线:: get(' / test',' Controller @ index');

    enter image description here

2 个答案:

答案 0 :(得分:0)

没有FDPF的经验,但你可以这样下载:

Route::get(
    'download/pdf/{pdf}',
    function ($pdf) {
        $file = // Get file
        return response()->download($file);
    }
);

或者只是来自您的控制器

return response()->download($pdf);

答案 1 :(得分:0)

用于保存,只需在输出调用字符串中指定输出路径和文件名

Fpdf::Output([string dest [, string name [, boolean isUTF8]]])  

但是,对于您的空白区域,在构建PDF文档时,可以使用以下之一的默认大小:A3,A4,A5,Letter,Legal,A4是默认值。但是,您也可以声明自定义大小。这很可能是你正在寻找的东西,因为你需要使用尺寸来获得你正在寻找的空白量。 FPDF先放出画布然后填充它,所以你的白色空间来自太大的画布。这可以在构造函数或AddPage中完成。

威盛构造函数:

//(L)andscape or (P)ortrait, unit type (mm millimeters), array size in mm
$pdf = new FPDF('L','mm',array(100,150));

VIA AddPage(必须是你想要的): 目前你有:

Fpdf::AddPage('L', array(60,90), 'A4');

然而,params应该是横向/纵向,预定义或自定义大小的数组,然后是旋转。所以试试这个:

Fpdf::AddPage('L', array(60,90));

现在你需要玩这些数字,而不仅仅是90,并缩短它以摆脱白色空间。