首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >循环遍历pdf文件中的值和显示

循环遍历pdf文件中的值和显示
EN

Stack Overflow用户
提问于 2010-04-07 01:57:20
回答 1查看 690关注 0票数 0

我已经编写了以下代码:如您所见,有一个for循环来遍历一些值并将它们显示在生成的pdf中。问题是所有的值都是在同一个地方写的。我已尝试插入新行,但似乎不起作用。有没有人能建议我怎么做?我是否需要编写一个嵌套的for循环,以便让它在不同的y位置取值?

代码语言:javascript
运行
复制
$pdf = pdf_new();

            // open a file
            pdf_open_file($pdf, "C:/xampp/htdocs/final/6.pdf");
            pdf_set_info($pdf, "Author", "");
            pdf_set_info($pdf, "Title", "");
            pdf_set_info($pdf, "Creator", "");
            pdf_set_info($pdf, "Subject", "");
            // start a new page (A4)
            $x = 595;
            $y = 842;
            pdf_begin_page($pdf, $x, $y);
            pdf_set_parameter($pdf, 'FontOutline', 'Arial=c:\windows\fonts\arial.ttf');
            pdf_setcolor($pdf, "stroke", "rgb", 0, 0, 0, 1.0);

            // get and use a font object
            $font = pdf_findfont($pdf, "Arial", "host", 1);

            pdf_setfont($pdf, $font, 10);

            // print text
            pdf_show_xy($pdf, "QUOTATION" , 250, $y - 60);
            pdf_show_xy($pdf, "Customer Name: " . $this->customer_details['first_name'] .
                " " . $this->customer_details['last_name'], 50, 770);
            pdf_show_xy($pdf, "Date: " . date("F j, Y, g:i a"), 50, 750);
            pdf_show_xy($pdf, "Number of items requested: " . $count_items_req, 50, 730);
            pdf_show_xy($pdf, "Number of items found: " . $count_items_found, 50, 710);


            // add an image under the text
            $image = $image = PDF_load_image($pdf, "png",
                "C:/xampp/htdocs/final/images/footer_logo.png", "");

            PDF_fit_image($pdf, $image, 50, 785, "");
            pdf_moveto($pdf, 20, 780);
            pdf_lineto($pdf, 575, 780);
            pdf_stroke($pdf);

            // draw another line near the bottom of the page
            pdf_moveto($pdf, 20, 50);
            pdf_lineto($pdf, 575, 50);
            pdf_stroke($pdf);

            //Draw the lines
            $offset = 184;
            $i = 0;
            pdf_moveto($pdf, 20, $y - 160);
            pdf_lineto($pdf, $x - 20, $y - 160);
            pdf_stroke($pdf);

            pdf_moveto($pdf, $x - 400, $y - 160);
            pdf_lineto($pdf, $x - 400, 80);
            pdf_stroke($pdf);

            pdf_moveto($pdf, $x - 200, $y - 160);
            pdf_lineto($pdf, $x - 200, 80);
            pdf_stroke($pdf);

            pdf_moveto($pdf, $x - 100, $y - 160);
            pdf_lineto($pdf, $x - 100, 80);
            pdf_stroke($pdf);

            pdf_continue_text($pdf, '');
            pdf_continue_text($pdf, '');

            pdf_show_xy($pdf, "Searched Item", 70, $y - 150);
            pdf_show_xy($pdf, "Searched Item", 70, $y - 150);


            pdf_show_xy($pdf, "Item name", 240, $y - 150);
            pdf_show_xy($pdf, "Item name", 240, $y - 150);
            pdf_show_xy($pdf, "Price", $x - 180, $y - 150);
            pdf_show_xy($pdf, "Price", $x - 180, $y - 150);
            pdf_show_xy($pdf, "Discounted Price", $x - 100, $y - 150);
            pdf_show_xy($pdf, "Discounted Price", $x - 100, $y - 150);
            for ($i = 0; $i < count($this->quotation_details); $i++)
            {

                 pdf_show_xy($pdf, $this->quotation_details[$i]['name_searched'] , 70, $y - 500);

            }


            // and write some text under it
            pdf_show_xy($pdf, "", 250, 35);


            // end page
            pdf_end_page($pdf);

            // close and save file
            pdf_close($pdf);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-04-07 02:45:17

您不需要嵌套循环,您只需要更改每次从$y中减去的值。你可以这样做:

代码语言:javascript
运行
复制
$placeholder = 500; // initial value
for ($i = 0; $i quotation_details); $i++)
{
  pdf_show_xy($pdf, $this->quotation_details[$i]['name_searched'] , 70, $y - $placeholder);
  $placeholder = $placeholder - 20;
}

你可能想要减去大于或小于20的值。尝试一下,找出一个看起来不错的值。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2587116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档