我正在尝试创建包含条形码的递增标签页面。我可以将条形码放入 PDF 中,并且可以将其叠加在 PDF 中的其他内容上(见下文)。但我不知道如何使用类似的东西将条形码分配给 pdf 的某个页面
Zend_Barcode::factory('code39', 'pdf',
$barcodeOptions, $rendererOptions)->setResource($page)->draw();
或者
$page = Zend_Barcode::factory('code39', 'pdf', $barcodeOptions,
$rendererOptions)->setResource($pdf)->draw();
在我的较大代码的上下文中设置上述两个片段时都会导致错误。再往下是没有错误地呈现的代码,但没有给我我需要的东西。
这个问题与Zend Framework Render Barcodes Into PDF Pages相同,从我所看到的情况来看,它从未得到有效的答案。
首先,为了让它工作,我将 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());
}
}