Php Zend Framework将条形码呈现到包含其他内容的多个PDF页面中
我正在尝试创建包含条形码的递增标签页。我可以将条形码转换成PDF格式,并将其叠加到PDF格式的其他内容上(见下文)。但我不知道如何使用类似的方式将条形码分配到pdf的特定页面Php Zend Framework将条形码呈现到包含其他内容的多个PDF页面中,php,zend-framework,pdf,barcode,Php,Zend Framework,Pdf,Barcode,我正在尝试创建包含条形码的递增标签页。我可以将条形码转换成PDF格式,并将其叠加到PDF格式的其他内容上(见下文)。但我不知道如何使用类似的方式将条形码分配到pdf的特定页面 Zend_Barcode::factory('code39', 'pdf', $barcodeOptions, $rendererOptions)->setResource($page)->draw(); 或 在我的较大代码的上下文中设置时,上述两个代码段都会导致错误。再往下看,代码呈现时没有错误,但没有提供
Zend_Barcode::factory('code39', 'pdf',
$barcodeOptions, $rendererOptions)->setResource($page)->draw();
或
在我的较大代码的上下文中设置时,上述两个代码段都会导致错误。再往下看,代码呈现时没有错误,但没有提供我需要的东西
这个问题和我所看到的问题是一样的,从我所看到的情况来看,这个问题从来没有得到一个有效的答案
首先,为了实现这一点,我将Zend版本1.11条形码库复制到我的Zend版本1.7.2库文件夹中。如代码所示,我还将一个免费的非等距字体复制到我的应用程序/库中
我可以创建标签的多页PDF。我可以将条形码放入PDF中。我不能做的是在多个页面上放置条形码。我会告诉你我在做什么,希望你能告诉我我做错了什么或者需要做什么
首先,我有一个小片段,将条形码放入PDF:
// A font path is mandatory for Barcode Pdf output.
// This application defines ROOT_DIR in index.php. Others may define APPLICATION_PATH.
// Monospaced fonts apparently don't work here.
$barcodeOptions = array(
'text' => '11111',
'font' => ROOT_DIR . '/library/Rbs/Barcode/FreeSerif.ttf'
);
$rendererOptions = array(
'topOffset' => 50,
'leftOffset' => 50
);
Zend_Barcode::factory('code39', 'pdf',
$barcodeOptions, $rendererOptions)->setResource($pdf)->draw();
然后我有这个(长)代码来创建标签。我在下面接近底部的地方评论了我尝试放置条形码的两个主要位置,但核心问题似乎是我不知道如何在PDF的某个页面上放置条形码:
public function labelsAction()
{
$request['starting_label_number'] = '100001';
$request['label_count'] = 32;
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$filename= 'files/inventory_labels.pdf';
$form['units_name'] = 'inches';
$form['units_factor'] = 72;
$form['margin_bottom'] = 0.5;
$form['margin_left'] = 0.19;
$form['label_width'] = 2.625;
$form['label_height'] = 1.0;
$form['label_spacing_column'] = 0.125;
$form['label_margin'] = 0.1;
$form['label_column_count'] = 3;
$form['label_row_count'] = 10;
try
{
// create PDF
$pdf = new Zend_Pdf();
// define font resource
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES_ROMAN);
$total_pages = ceil($request['label_count'] / ($form['label_column_count'] * $form['label_row_count']));
$current_label_number = $request['starting_label_number'];
// Create each page
for ($page_number = 1; $page_number <= $total_pages; $page_number++)
{
// create A4 page
$page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_LETTER);
// write text to page
// Fill up each page with labels or blanks
for ($row = 1; $row <= $form['label_row_count']; $row++)
{
for ($column = 1; $column <= $form['label_column_count']; $column++)
{
if ($current_label_number - $request['starting_label_number'] < $request['label_count'])
{
$label_center_y =
(
$form['margin_bottom']
+ ($form['label_row_count'] - $row + 0.5) * $form['label_height']
) * $form['units_factor'];
$label_center_x =
(
$form['margin_left']
+ ($column - 0.5) * $form['label_width']
+ ($column - 1) * $form['label_spacing_column']
) * $form['units_factor'];
// Get our bearings $xleft, $ybottom, $xright, $ytop, $filltype
$page->drawRectangle(
$label_center_x - $form['label_width'] * $form['units_factor'] / 2,
$label_center_y - $form['label_height'] * $form['units_factor'] / 2,
$label_center_x + $form['label_width'] * $form['units_factor'] / 2,
$label_center_y + $form['label_height'] * $form['units_factor'] / 2,
Zend_Pdf_Page::SHAPE_DRAW_STROKE
);
// define image resource
$image = Zend_Pdf_Image::imageWithPath('images/new/logo.jpg');
// write image to page $xleft, $ybottom, $xright, $ytop
$image_width = 125;
$image_height = 30;
$image_y_offset = 13;
$page->drawImage(
$image,
$label_center_x - $image_width / 2,
$label_center_y - $image_height / 2 + $image_y_offset,
$label_center_x + $image_width / 2,
$label_center_y + $image_height / 2 + $image_y_offset
);
$text_width = 108;
$page->setFont($font, 10)
->drawText('www.pristineauction.com', $label_center_x - $text_width / 2, $label_center_y - 10);
$text_width = 68;
$page->setFont($font, 22)
->drawText($current_label_number, $label_center_x - $text_width / 2, $label_center_y - 32);
$current_label_number += 1;
}
}
}
/* If I insert the barcode creator here inside the label loop, I get a barcode all by itself on the first PDF page, with labels following on subsequent pages. Not bad, but not right. */
// add page to document
$pdf->pages[] = $page;
}
/* If I insert the barcode creator here outside the label loop, I get a barcode superimposed on the first PDF page. Not bad, but not in page loop, and not repeating. */
// save as file
$pdf->save($filename);
$this->_redirect('/' . $filename);
} catch (Zend_Pdf_Exception $e) {
die ('PDF error: ' . $e->getMessage());
} catch (Exception $e) {
die ('Application error: ' . $e->getMessage());
}
}
public function labelAction()
{
$request['starting_label_number']='100001';
$request['label_count']=32;
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$filename='files/inventory_labels.pdf';
$form['units_name']='inches';
$form['units\u factor']=72;
$form['margin_bottom']=0.5;
$form['margin_left']=0.19;
$form['label_width']=2.625;
$form['label_height']=1.0;
$form['label\u space\u column']=0.125;
$form['label_margin']=0.1;
$form['label\u column\u count']=3;
$form['label\u row\u count']=10;
尝试
{
//创建PDF
$pdf=new Zend_pdf();
//定义字体资源
$font=Zend_Pdf_font::fontWithName(Zend_Pdf_font::font_TIMES_ROMAN);
$total_pages=ceil($request['label_count']/($form['label_column_count']*$form['label_row_count']);
$current_label_number=$request['starting_label_number'];
//创建每个页面
对于($page\u number=1;$page\u number setFont($font,10)
->drawText('www.pristineauction.com',$label\u center\u x-$text\u width/2,$label\u center\u y-10);
$text_width=68;
$page->setFont($font,22)
->drawText($current\u label\u number,$label\u center\u x-$text\u width/2,$label\u center\u y-32);
$current_label_number+=1;
}
}
}
/*如果我在标签循环中插入条形码创建者,我会在第一个PDF页面上自动获得条形码,随后的页面上会出现标签。不错,但不正确。*/
//向文档中添加页面
$pdf->pages[]=$pages;
}
/*如果我将条形码创建者插入标签循环之外,则会在第一个PDF页面上叠加一个条形码。不错,但不会在页面循环中,也不会重复。*/
//另存为文件
$pdf->save($filename);
$this->_重定向('/'.$filename);
}捕获(Zend_Pdf_例外$e){
die('PDF错误:'.$e->getMessage());
}捕获(例外$e){
die('应用程序错误:'.$e->getMessage());
}
}
我找到了答案,多亏了
您可以提供页面索引作为参数!很高兴终于知道了这一点
好的方面是:
// Create Pdf definition
$pdf = new Zend_Pdf();
// Define font resource for Pdf library
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES_ROMAN);
// Must set a TTF for Barcode library
// This application defines ROOT_DIR in index.php. Others may define APPLICATION_PATH.
Zend_Barcode::setBarcodeFont(ROOT_DIR . '/library/Rbs/Barcode/FreeSerif.ttf');
// Create the Pdf library elements for each page and add it to the document
// before printing the Barcode elements onto that page
for ($page_index = 0; $page_index <= $last_page_index; $page_index++)
{
// create a new Pdf page
$page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_LETTER);
// write Pdf elements to the page
// add page to document
$pdf->pages[] = $page;
}
// Add barcodes to each page after it is added.
for ($page_index = 0; $page_index <= $last_page_index; $page_index++)
{
$barcodeOptions = array(
'text' => $current_label_number
);
$rendererOptions = array(
'topOffset' => $label_center_y + 3,
'leftOffset' => $label_center_x + 12
);
Zend_Barcode::factory('code39', 'pdf',
$barcodeOptions, $rendererOptions)->setResource($pdf, $page_index)->draw();
$current_label_number += 1;
}
// save as file
$pdf->save($filename);
//创建Pdf定义
$pdf=new Zend_pdf();
//为Pdf库定义字体资源
$font=Zend_Pdf_font::fontWithName(Zend_Pdf_font::font_TIMES_ROMAN);
//必须为条形码库设置TTF
//此应用程序在index.php中定义根目录。其他人可以定义应用程序路径。
Zend_条形码::setBarcodeFont(ROOT_DIR.'/library/Rbs/Barcode/freeerif.ttf');
//为每个页面创建Pdf库元素并将其添加到文档中
//在将条形码元素打印到该页面之前
对于($page_index=0;$page_index pages[]=$page;
}
//添加后,将条形码添加到每页。
对于($page\u index=0;$page\u index$当前标签号)
);
$renderoptions=array(
“topOffset”=>$label\u center\u y+3,
“leftOffset”=>$label\u center\u x+12
);
Zend_条形码::工厂('code39','pdf',
$barcodeOptions,$RenderOptions)->setResource($pdf,$page_index)->draw();
$current_label_number+=1;
}
//另存为文件
$pdf->save($filename);
整个标签制造商:
// By Tom Haws 2012-10-03
// Prints labels with barcodes to pdf
public function labelsAction()
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$request['start_number'] = max(1, $this->_request->getParam('start-number'));
$request['label_count'] = $this->_request->getParam('label-count');
$inventory_number = $request['start_number'];
$filename= 'files/inventory_labels.pdf';
$form['units_name'] = 'inches';
$form['units_factor'] = 72;
$form['margin_top'] = 0.5;
$form['margin_bottom'] = 0.5;
$form['margin_left'] = 0.19;
$form['label_width'] = 2.625;
$form['label_height'] = 1.0;
$form['label_spacing_column'] = 0.125;
$form['label_margin'] = 0.1;
$form['label_column_count'] = 3;
$form['label_row_count'] = 10;
$last_page_index = ceil($request['label_count'] / ($form['label_column_count'] * $form['label_row_count'])) - 1;
$current_label_number = $request['start_number'];
try
{
// Create PDF
$pdf = new Zend_Pdf();
// Define font resource for Pdf library
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES_ROMAN);
// Must set a TTF for Barcode library
Zend_Barcode::setBarcodeFont(ROOT_DIR . '/library/Rbs/Barcode/FreeSerif.ttf');
// Create the Pdf library elements for each page and add it to the document
// before printing the Barcode elements onto that page
for ($page_index = 0; $page_index <= $last_page_index; $page_index++)
{
// create the new Pdf page
$page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_LETTER);
// write Pdf elements to the page
// Fill up each page with labels or blanks
for ($row = 1; $row <= $form['label_row_count']; $row++)
{
for ($column = 1; $column <= $form['label_column_count']; $column++)
{
if ($current_label_number - $request['start_number'] < $request['label_count'])
{
// Note that Pdf library uses a coordinate system with origin at bottom left
$label_center_y =
(
$form['margin_bottom']
+ ($form['label_row_count'] - $row + 0.5) * $form['label_height']
) * $form['units_factor'];
$label_center_x =
(
$form['margin_left']
+ ($column - 0.5) * $form['label_width']
+ ($column - 1) * $form['label_spacing_column']
) * $form['units_factor'];
// Draw a design guidance frame for each label.
// $xleft, $ybottom, $xright, $ytop, $filltype
/*
$page->drawRectangle(
$label_center_x - $form['label_width'] * $form['units_factor'] / 2,
$label_center_y - $form['label_height'] * $form['units_factor'] / 2,
$label_center_x + $form['label_width'] * $form['units_factor'] / 2,
$label_center_y + $form['label_height'] * $form['units_factor'] / 2,
Zend_Pdf_Page::SHAPE_DRAW_STROKE
); */
// define image resource
$image = Zend_Pdf_Image::imageWithPath('images/new/logo.jpg');
// write image to page $xleft, $ybottom, $xright, $ytop
$image_width = 125;
$image_height = 30;
$image_y_offset = 20;
$page->drawImage(
$image,
$label_center_x - $image_width / 2,
$label_center_y - $image_height / 2 + $image_y_offset,
$label_center_x + $image_width / 2,
$label_center_y + $image_height / 2 + $image_y_offset
);
$text_width = 108;
$page->setFont($font, 10)
->drawText('www.pristineauction.com', $label_center_x - $text_width / 2, $label_center_y - 1);
$text_width = 68;
$page->setFont($font, 22)
->drawText($current_label_number, $label_center_x - 72, $label_center_y - 25);
$current_label_number += 1;
}
}
}
// add page to document
$pdf->pages[] = $page;
}
// Add barcodes to pages of document.
$current_label_number = $request['start_number'];
for ($page_index = 0; $page_index <= $last_page_index; $page_index++)
{
for ($row = 1; $row <= $form['label_row_count']; $row++)
{
for ($column = 1; $column <= $form['label_column_count']; $column++)
{
// Note that barcodes use a coordinate system with origin at top left
$label_center_y =
(
$form['margin_top']
+ ($row - 0.5) * $form['label_height']
) * $form['units_factor'];
$label_center_x =
(
$form['margin_left']
+ ($column - 0.5) * $form['label_width']
+ ($column - 1) * $form['label_spacing_column']
) * $form['units_factor'];
// A font path is mandatory for Barcode Pdf output.
// This application defines ROOT_DIR in index.php. Others may define APPLICATION_PATH.
$barcodeOptions = array(
'text' => $current_label_number
);
$rendererOptions = array(
'topOffset' => $label_center_y + 3,
'leftOffset' => $label_center_x + 12
);
Zend_Barcode::factory('code39', 'pdf',
$barcodeOptions, $rendererOptions)->setResource($pdf, $page_index)->draw();
$current_label_number += 1;
}
}
}
// save as file
$pdf->save($filename);
$this->_redirect('/' . $filename);
} catch (Zend_Pdf_Exception $e) {
die ('PDF error: ' . $e->getMessage());
} catch (Exception $e) {
die ('Application error: ' . $e->getMessage());
}
}
//作者:汤姆·霍斯2012-10-03
//将带有条形码的标签打印为pdf格式
公共功能标签行动()
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$request['start\u number']=max(1,$this->\u request->getParam('start-number');
$request['label\u count']=$this->\u request->getParam('label-count');
$inventory_number=$request['start_number'];
$filename='files/inventory_labels.pdf';
$form['units_name']='inches';
$form['units\u factor']=72;
$form['margin_top']=0.5;
$form['margin_bottom']=0.5;
$form['margin_left']=0.19;
$form['label_width']=2.625;
$form['label_height']=1.0;
$form['label\u space\u column']=0.125;
$form['label_margin']=0.1;
$form['label\u column\u count']=3;
$form['label\u row\u count']=10;
$last\u page\u index=ceil($request['label\u count']/($form['label\u coln
// By Tom Haws 2012-10-03
// Prints labels with barcodes to pdf
public function labelsAction()
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$request['start_number'] = max(1, $this->_request->getParam('start-number'));
$request['label_count'] = $this->_request->getParam('label-count');
$inventory_number = $request['start_number'];
$filename= 'files/inventory_labels.pdf';
$form['units_name'] = 'inches';
$form['units_factor'] = 72;
$form['margin_top'] = 0.5;
$form['margin_bottom'] = 0.5;
$form['margin_left'] = 0.19;
$form['label_width'] = 2.625;
$form['label_height'] = 1.0;
$form['label_spacing_column'] = 0.125;
$form['label_margin'] = 0.1;
$form['label_column_count'] = 3;
$form['label_row_count'] = 10;
$last_page_index = ceil($request['label_count'] / ($form['label_column_count'] * $form['label_row_count'])) - 1;
$current_label_number = $request['start_number'];
try
{
// Create PDF
$pdf = new Zend_Pdf();
// Define font resource for Pdf library
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES_ROMAN);
// Must set a TTF for Barcode library
Zend_Barcode::setBarcodeFont(ROOT_DIR . '/library/Rbs/Barcode/FreeSerif.ttf');
// Create the Pdf library elements for each page and add it to the document
// before printing the Barcode elements onto that page
for ($page_index = 0; $page_index <= $last_page_index; $page_index++)
{
// create the new Pdf page
$page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_LETTER);
// write Pdf elements to the page
// Fill up each page with labels or blanks
for ($row = 1; $row <= $form['label_row_count']; $row++)
{
for ($column = 1; $column <= $form['label_column_count']; $column++)
{
if ($current_label_number - $request['start_number'] < $request['label_count'])
{
// Note that Pdf library uses a coordinate system with origin at bottom left
$label_center_y =
(
$form['margin_bottom']
+ ($form['label_row_count'] - $row + 0.5) * $form['label_height']
) * $form['units_factor'];
$label_center_x =
(
$form['margin_left']
+ ($column - 0.5) * $form['label_width']
+ ($column - 1) * $form['label_spacing_column']
) * $form['units_factor'];
// Draw a design guidance frame for each label.
// $xleft, $ybottom, $xright, $ytop, $filltype
/*
$page->drawRectangle(
$label_center_x - $form['label_width'] * $form['units_factor'] / 2,
$label_center_y - $form['label_height'] * $form['units_factor'] / 2,
$label_center_x + $form['label_width'] * $form['units_factor'] / 2,
$label_center_y + $form['label_height'] * $form['units_factor'] / 2,
Zend_Pdf_Page::SHAPE_DRAW_STROKE
); */
// define image resource
$image = Zend_Pdf_Image::imageWithPath('images/new/logo.jpg');
// write image to page $xleft, $ybottom, $xright, $ytop
$image_width = 125;
$image_height = 30;
$image_y_offset = 20;
$page->drawImage(
$image,
$label_center_x - $image_width / 2,
$label_center_y - $image_height / 2 + $image_y_offset,
$label_center_x + $image_width / 2,
$label_center_y + $image_height / 2 + $image_y_offset
);
$text_width = 108;
$page->setFont($font, 10)
->drawText('www.pristineauction.com', $label_center_x - $text_width / 2, $label_center_y - 1);
$text_width = 68;
$page->setFont($font, 22)
->drawText($current_label_number, $label_center_x - 72, $label_center_y - 25);
$current_label_number += 1;
}
}
}
// add page to document
$pdf->pages[] = $page;
}
// Add barcodes to pages of document.
$current_label_number = $request['start_number'];
for ($page_index = 0; $page_index <= $last_page_index; $page_index++)
{
for ($row = 1; $row <= $form['label_row_count']; $row++)
{
for ($column = 1; $column <= $form['label_column_count']; $column++)
{
// Note that barcodes use a coordinate system with origin at top left
$label_center_y =
(
$form['margin_top']
+ ($row - 0.5) * $form['label_height']
) * $form['units_factor'];
$label_center_x =
(
$form['margin_left']
+ ($column - 0.5) * $form['label_width']
+ ($column - 1) * $form['label_spacing_column']
) * $form['units_factor'];
// A font path is mandatory for Barcode Pdf output.
// This application defines ROOT_DIR in index.php. Others may define APPLICATION_PATH.
$barcodeOptions = array(
'text' => $current_label_number
);
$rendererOptions = array(
'topOffset' => $label_center_y + 3,
'leftOffset' => $label_center_x + 12
);
Zend_Barcode::factory('code39', 'pdf',
$barcodeOptions, $rendererOptions)->setResource($pdf, $page_index)->draw();
$current_label_number += 1;
}
}
}
// save as file
$pdf->save($filename);
$this->_redirect('/' . $filename);
} catch (Zend_Pdf_Exception $e) {
die ('PDF error: ' . $e->getMessage());
} catch (Exception $e) {
die ('Application error: ' . $e->getMessage());
}
}