I was testing the module out and the PDF was outputting great! However, for each record the fields were displayed in the mailing label PDF as a list looking like:
First Name
Last Name
Address 1
City
State
Zip
In the view this is generated from, the fields are formatted to be output as:
First Name Last Name
Address 1
City State Zip
I'm just wondering if there is a way to format the PDF mailing label output display fields other than a vertical field list? Maybe I missed a views setting.
Thanks!
| Comment | File | Size | Author |
|---|---|---|---|
| #24 | screenshot.jpg | 237.92 KB | jeefers |
| #18 | inline_fields_only.patch | 3.11 KB | letapjar |
| #15 | inline_fix2.patch | 3.14 KB | letapjar |
| #13 | inline_fix.patch | 3.14 KB | letapjar |
| #8 | mailing_label_revised.patch | 9.21 KB | letapjar |
Comments
Comment #1
acouch commentedThis would be nice. It would require grabbing the 'inline' info from fields in the view. Technically not too hard. I don't have time to add this right now, but would accept a patch.
Comment #2
vannus commentedComment #3
vannus commented+subscribe
Comment #4
letapjar commentedHere is a potential solution. I'm not familiar with CVS so I did it by hand and have posted my modified versions of mailing_label_plugin_diaplay_attachment.inc and mailing_label.module below. (zip file of the module with the changes attached to this post). *** edit - in my haste, I left 3 unnecessary files in the attached zip - these are titled: "options_array", "file object vardump" and "dropped text.txt" - they were part of my debugging process and left in by accident (had I been using some form of CVS this wouldn't have happened - still have to learn CVS tools. - my apologies for the oversight and for the double-post notification you'll receive if you're subscribed to this issue***
It would be great if someone else could try it out and verify that it produces the desired results.
Note - the inline fields property is a little different than for a HTML rendered page in that you can have a single field marked as inline and this module will correctly inline just that field with the preceding one. If you check two fields in a row as inline - BOTH will be inlined.
This implementation does not honor the 'separator' setting from the views form - it forces all inlined fields to be separated by a space instead.
I added a function to the pluging attachment to allow the module to use exposed filters - helps when you have very large lists and want to be able to choose sublists to output. Just be sure to click the apply button when making a change so the plugin attachment does its thing.
Also, it looks like there was an unnecessary loop in the plugin's render() function. I've removed it - but if it was necessary for some reason I could not see - please do comment.
I also added a form field in the module to give a prefix to the file name - on my linux box I had no-way to specify a "save-as" file name for the label pdf file so every set of labels would be named the same.
<?php
// $Id: mailing_label_plugin_display_attachment.inc,v 1.3.2.3 2009/10/25 00:53:53 acouch Exp $
/**
* The plugin that handles Mailing label attachments.
*
* Creates a special attachment for this purpose only.
*/
class mailing_label_plugin_display_attachment extends views_plugin_display_attachment {
function defaultable_sections($section = NULL) {
if (in_array($section, array('row_options', 'row_plugin', 'items_per_page'))) {
//if (in_array($section, array('items_per_page'))) {
return FALSE;
}
return parent::defaultable_sections($section);
}
/**
* 2/20/2010 Added this function to catch exposed filters
* makes a single page useful in generating
* labels for various lists based on exposed filters
*/
function option_definition () {
$options = parent::option_definition();
$options['inherit_exposed_filters'] = array('default' => TRUE);
return $options;
}
function options(&$display) {
parent::options($display);
$display->display_options['items_per_page'] = 0;
$display->display_options['row_plugin'] = '';
$display->display_options['defaults']['style_options'] = FALSE;
$display->display_options['defaults']['items_per_page'] = FALSE;
$display->display_options['defaults']['row_plugin'] = FALSE;
$display->display_options['defaults']['row_options'] = FALSE;
}
function render() {
$current_display = $this->view->current_display;
$fields = $this->view->display[$current_display]->handler->handlers['field'];
$view=$this->view;
$i=0;
//catch the inline fields from the defualt view
$row_options = $this->view->display[$current_display]->handler->options['row_options'];
if (!empty($row_options)){
$inlines=$row_options['inline'];
if (!empty($inlines)){
//there are inline fields to process so do it
$allFields = array_keys($view->field);//grab labels for all the fields
$prepends=array_flip(array_diff_key(array_flip($allFields),$inlines));// find the fields not in the inlines array
if ($prepends[0]==$allFields[0]){
$x = array_shift($prepends);//omit the first field since it's not necessary to prepend it
}
$allFields=array_flip($allFields);
foreach($allFields as $id=>$new_id){
$new_id=$id;
if (in_array($id,$prepends)){
$new_id.='_mailing_label_newline'; //change the key name for this field so when we process the form we know to start on a new line
}
$allFields[$id]=$new_id;
}
}
}
foreach ($view->result as $result) {
//foreach ($result as $rid => $output) { //unnecessary looping here - remove it
foreach ($view->field as $id => $field) {
//$results[$i][] = check_plain(strip_tags(($view->field[$id]->theme($result)))); -- view-object access unneeded - use the iterator
$results[$i][$allFields[$id]] = check_plain(strip_tags(($field->theme($result))));
}
//} removed extra loop over result set
$i++;
}
return mailing_label_pdf_form($results);
}
}
***********************************************************************************
<?php
// $Id: mailing_label.module,v 1.4.2.5 2009/10/25 02:05:08 acouch Exp $
/**
* Implementation of hook_views_api().
*
*/
function mailing_label_views_api(){
return array(
'api' => 2,
);
}
/**
* Implementation of hook_form().
*
*/
function mailing_label_form($form_state, $results) {
$options = array('5160' => t('Avery 5160'),
'5161' => t('Avery 5161'),
'5162' => t('Avery 5162'),
'5163' => t('Avery 5163'),
'8600' => t('Avery 8600'),
'L7160' => t('Avery L7160'),
'L7161' => t('Avery L7161'),
'L7163' => t('Avery L7163'),
);
$form['choices'] = array(
'#type' => 'select',
'#title' => t('Select a mailing label size'),
'#options' => $options,
'#tree' => TRUE,
);
$form['description'] = array (
'#type' => 'hidden',
'#tree' => $results,
);
/*
* 2-20-2010 Added form field to give a file nme prefix for the output file
*/
$form['filename'] = array (
'#title' => t('File Name Prefix'),
'#type' => 'textfield',
'#description' => 'Enter a file name prefix for the mailing label output file',
'#maxlength' => 20,
'#required' => FALSE,
'#size' => 20,
);
$form['submit'] = array('#type' => 'submit', '#value' => t('Print PDF'));
return $form;
}
/**
* Implementation of hook_form_submit().
*
*/
function mailing_label_form_submit($form, &$form_state) {
$results = $form['description']['#tree'];
$format = $form_state['values']['choices'];
$file_name = $form_state['values']['filename'];
mailing_label_create_label($results, $format, $file_name);
}
/**
* Call hook_form().
*
*/
function mailing_label_pdf_form($results) {
return drupal_get_form('mailing_label_form',$results);
}
/**
* Create label function
*
*/
function mailing_label_create_label(&$contactRows, &$format, $file_name) {
$path = drupal_get_path('module', 'mailing_label');
require_once ($path . '/mailing_label-ufpdf.php');
$pdf = new Mailing_Label_PDF_Label($format,'mm');
$pdf->Open();
$pdf->AddPage();
$pdf->AddFont('DejaVu Sans', '', 'DejaVuSans.php');
$pdf->SetFont('DejaVu Sans');
//build contact string that needs to be printed
$val = null;
foreach ($contactRows as $row => $value) {
foreach ($value as $k => $v) {
if (!stristr($k,'_mailing_label_newline')){
$val .= ' '.$v;//the field is not flagged as starting on a new line - prepend a space
}
else{
$val.="\n".$v;//field flagged as starting on a new line - prepend a newline
}
}
$pdf->AddPdfLabel($val);
$val = '';
}
$pdf->Output( $file_name. '_label_' . $format . '.pdf', 'D' );
exit;
}
Comment #5
letapjar commentedThe mailing label modifications in the previous post break when the labels come from a relationship as opposed to from the base table of the underlying view. The zip file attached below contains an updated version of the module with my changes that handles this case correctly.
Also, a minor edit to the mailing_label_ufpdf file was made. Previously, if you printed out a set of labels which exactly filled a number of label pages, an extra blank page was included in the pdf file. This has been fixed by this edit.
Comment #6
letapjar commentedPatch to mailing_label module per the above changes:
diff -up /home/rajesh/drupal_modules/mailing_label/mailing_label.module ./mailing_label/mailing_label.module
--- /home/rajesh/drupal_modules/mailing_label/mailing_label.module 2009-10-24 22:05:08.000000000 -0400
+++ ./mailing_label/mailing_label.module 2010-03-17 08:50:56.000000000 -0400
@@ -24,6 +24,7 @@ function mailing_label_form($form_state,
'5161' => t('Avery 5161'),
'5162' => t('Avery 5162'),
'5163' => t('Avery 5163'),
+ '5167' => t('Avery 5167 Return Address'),
'8600' => t('Avery 8600'),
'L7160' => t('Avery L7160'),
'L7161' => t('Avery L7161'),
@@ -35,12 +36,26 @@ function mailing_label_form($form_state,
'#title' => t('Select a mailing label size'),
'#options' => $options,
'#tree' => TRUE,
+ '#description' => t('Check the box of the labels you will be using - usually Avery 5160')
);
$form['description'] = array (
'#type' => 'hidden',
'#tree' => $results,
);
- $form['submit'] = array('#type' => 'submit', '#value' => t('Print PDF'));
+ /*
+ * 2-20-2010 Added form field to give a file nme prefix for the output file
+ */
+ $form['filename'] = array (
+ '#title' => t('File Name'),
+ '#type' => 'textfield',
+ '#description' => 'Enter a file name for the mailing label output file',
+ '#maxlength' => 20,
+ '#required' => FALSE,
+ '#size' => 20,
+ );
+
+ $form['submit'] = array('#type' => 'submit', '#value' => t('Make My Labels'),
+ '#description'=>t('This will make a PDF file for you to download and use to print your labels'));
return $form;
}
@@ -52,7 +67,8 @@ function mailing_label_form($form_state,
function mailing_label_form_submit($form, &$form_state) {
$results = $form['description']['#tree'];
$format = $form_state['values']['choices'];
- mailing_label_create_label($results, $format);
+ $file_name = $form_state['values']['filename'];
+ mailing_label_create_label($results, $format, $file_name);
}
@@ -68,13 +84,13 @@ function mailing_label_pdf_form($results
* Create label function
*
*/
-function mailing_label_create_label(&$contactRows, &$format) {
+function mailing_label_create_label(&$contactRows, &$format, $file_name) {
$path = drupal_get_path('module', 'mailing_label');
require_once ($path . '/mailing_label-ufpdf.php');
$pdf = new Mailing_Label_PDF_Label($format,'mm');
$pdf->Open();
- $pdf->AddPage();
+ //$pdf->AddPage();
$pdf->AddFont('DejaVu Sans', '', 'DejaVuSans.php');
$pdf->SetFont('DejaVu Sans');
@@ -82,12 +98,17 @@ function mailing_label_create_label(&$co
$val = null;
foreach ($contactRows as $row => $value) {
foreach ($value as $k => $v) {
- $val .= "$v\n";
+ if (!stristr($k,'_mailing_label_newline')){
+ $val .= ' '.$v;//the field is not flagged as starting on a new line - prepend a space
+ }
+ else{
+ $val.="\n".$v;//field flagged as starting on a new line - prepend a newline
+ }
}
$pdf->AddPdfLabel($val);
$val = '';
}
- $pdf->Output( 'label_' . $format . '.pdf', 'D' );
+ $pdf->Output( $file_name. '_label_' . $format . '.pdf', 'D' );
exit;
}
diff -up /home/rajesh/drupal_modules/mailing_label/mailing_label_plugin_display_attachment.inc ./mailing_label/mailing_label_plugin_display_attachment.inc
--- /home/rajesh/drupal_modules/mailing_label/mailing_label_plugin_display_attachment.inc 2009-10-24 20:53:53.000000000 -0400
+++ ./mailing_label/mailing_label_plugin_display_attachment.inc 2010-03-23 14:53:25.000000000 -0400
@@ -2,19 +2,30 @@
// $Id: mailing_label_plugin_display_attachment.inc,v 1.3.2.3 2009/10/25 00:53:53 acouch Exp $
/**
- * The plugin that handles date navigation attachments.
+ * The plugin that handles Mailing label attachments.
*
* Creates a special attachment for this purpose only.
*/
class mailing_label_plugin_display_attachment extends views_plugin_display_attachment {
function defaultable_sections($section = NULL) {
- if (in_array($section, array('row_options', 'row_plugin', 'items_per_page'))) {
+ if (in_array($section, array('row_options', 'row_plugin', 'items_per_page'))) {
+ //if (in_array($section, array('items_per_page'))) {
return FALSE;
}
return parent::defaultable_sections($section);
}
-
+/**
+* 2/20/2010 Added this function to catch exposed filters
+* makes a single page useful in generating
+* labels for various lists based on exposed filters
+*/
+ function option_definition () {
+ $options = parent::option_definition();
+ $options['inherit_exposed_filters'] = array('default' => TRUE);
+ return $options;
+ }
+
function options(&$display) {
parent::options($display);
$display->display_options['items_per_page'] = 0;
@@ -22,7 +33,7 @@ class mailing_label_plugin_display_attac
$display->display_options['defaults']['style_options'] = FALSE;
$display->display_options['defaults']['items_per_page'] = FALSE;
$display->display_options['defaults']['row_plugin'] = FALSE;
- $display->display_options['defaults']['row_options'] = FALSE;
+ $display->display_options['defaults']['row_options'] = FALSE;
}
function render() {
@@ -30,15 +41,44 @@ class mailing_label_plugin_display_attac
$fields = $this->view->display[$current_display]->handler->handlers['field'];
$view=$this->view;
$i=0;
+ //catch the inline fields from the defualt view
+ $row_options = $this->view->display[$current_display]->handler->options['row_options'];
+
+ //if the row optins are left as defualts the variable may be null from above so check that and grab default value
+ if (empty($row_options)){
+ $row_options = $this->view->display['default']->handler->options['row_options'];
+ }
+ $allFields = array_keys($fields); //grab labels for all the fields - stuff them in a number indexed array
+
+ if (!empty($row_options)){ //if there are no inline fields then we do nothing
+ $inlines=$row_options['inline'];
+ if (!empty($inlines)){
+ //there are inline fields to process so do it
+ $prepends=array_flip(array_diff_key(array_flip($allFields),$inlines));// find the fields not in the inlines array
+ if ($prepends[0]==$allFields[0]){
+ $x = array_shift($prepends);//omit the first field since it's not necessary to prepend it
+ }
+ $allFields=array_flip($allFields); //flip keys and values
+ foreach($allFields as $id=>$new_id){
+ $new_id=$id; //get rid of numeric values and assign field names again
+ if (in_array($id,$prepends)){
+ $new_id.='_mailing_label_newline'; //change the key name for this field so when we process the form we know to start on a new line
+ }
+ $allFields[$id]=$new_id; //the values of this array are the field keys used in the result set
+ }
+ }
+ }
+
+
foreach ($view->result as $result) {
- foreach ($result as $rid => $output) {
foreach ($view->field as $id => $field) {
- $results[$i][$id] = check_plain(strip_tags(($view->field[$id]->theme($result))));
+ $results[$i][$allFields[$id]] = check_plain(strip_tags(($field->theme($result))));
+
}
- }
+
$i++;
}
-
+
return mailing_label_pdf_form($results);
}
diff -up /home/rajesh/drupal_modules/mailing_label/mailing_label-ufpdf.php ./mailing_label/mailing_label-ufpdf.php
--- /home/rajesh/drupal_modules/mailing_label/mailing_label-ufpdf.php 2009-10-24 20:53:53.000000000 -0400
+++ ./mailing_label/mailing_label-ufpdf.php 2010-03-17 08:46:46.000000000 -0400
@@ -53,6 +53,10 @@
'lMargin' => 0.148, 'tMargin' => 0.5, 'NX' => 2, 'NY' => 3,
'SpaceX' => 0.2031, 'SpaceY' => 0, 'width' => 4.0, 'height' => 3.33,
'font-size' => 12),
+ '5167' => array('name' => '5167', 'paper-size' => 'letter', 'metric' => 'mm',
+ 'lMargin' => 7.62, 'tMargin' => 12.7, 'NX' => 4, 'NY' => 20,
+ 'SpaceX' => 7.62, 'SpaceY' => 0, 'width' => 44.45, 'height' => 12.7,
+ 'font-size' => 6),
'8600' => array('name' => '8600', 'paper-size' => 'letter', 'metric' => 'mm',
'lMargin' => 7.1, 'tMargin' => 19, 'NX' => 3, 'NY' => 10,
'SpaceX' => 9.5, 'SpaceY' => 3.1, 'width' => 66.6,
@@ -174,6 +178,12 @@
* function to Print a label
*/
function AddPdfLabel($texte) {
+
+ if (($this->countX ==0) and ($this->countY==0)) {
+ $this->AddPage();
+ }
+
+
$posX = $this->marginLeft+($this->countX*($this->width+$this->xSpace));
$posY = $this->marginTop+($this->countY*($this->height+$this->ySpace));
$this->SetXY($posX+3, $posY+3);
@@ -195,11 +205,7 @@
$this->countY=0;
}
- // We are in a new page, then we must add a page
- if (($this->countX ==0) and ($this->countY==0)) {
- $this->AddPage();
- }
- }
+ }
}
Common subdirectories: /home/rajesh/drupal_modules/mailing_label/ufpdf and ./mailing_label/ufpdf
Comment #7
letapjar commentedOk one last shot.
some people may not like the approach of the above patch because it uses re-naming of the array keys to pass along the style information.
This patch does the formatting of the data within the plugin display attachment itself. Data filed that start on a new line are prepended with a newline character. fields that are inline are prepended with a space. The very first field of every record is not prepended with anything.
hopefully this is a more "Drupal" way to do things.
Comment #8
letapjar commentedSorry for the additional post - I'm changing the status to needs review for the patch - the patch attched here is the same as the one in post #7 above.
Comment #9
tom_buytaert commentedWhy did you change results to result in this piece of code?
It's a pity you changed a lot of other things too in this patch. For example you added a label format, and added possibility to change filename. Which means I have to get rid of all those extra changes, just to get the patch itself..
Comment #10
newToo commentedI can't seem to get the fields to display "inline", I have the fields selected in view - the page view is inline, but the labels only show the last field added in view but none of the others. Any suggestions?
Comment #11
newToo commentedAfter uninstalling the module and re-installing a couple times, I finally got the inline to work. Any way to be able to add an image to the label inline with text? So, it would be a logo then beside it would be a name and address?
Thanks for the inline fix!
Comment #12
jeefers commenteddid someone only fix the inline issue??
Comment #13
letapjar commentedHere is a patch for fixing ONLY the inline fields issue - note this will still add an extra page to the pdf when the # labels exactly matches up with the end of a page. etc.
but it shoudl accurately catch the inline fields property and inline those fields.
Comment #14
jeefers commentedI applied the inline patch but now im getting
PHP Parse error: syntax error, unexpected T_VARIABLE in /sitepath/modules/mailing_label/mailing_label_plugin_display_attachment.inc on line 69
Comment #15
letapjar commentedmy appologies,
line 68 was missing a semicolon.
attached patch should work.
Comment #16
jeefers commentedletapjar: Thanks for all your help... Can i make one last request.... Can you add another patch or update this one so that it will exclude fields that are selected to be excluded in the view. If you can do that id be more than happy to send you $30 for all your troubles.
Comment #17
letapjar commentedThis request have been moved to a separate issue and a patch has been posted here
Comment #18
letapjar commentedThe patch in #16 above had a typo in a variable name which caused an error. Further, in the case where no fields were inlined - this patch produced the wrong results.
the attached patch resolves both of these issues - (note if you already applied the patch in 16 you should re-install the module before applying this patch since the patch is relative to the module code on drupal.org)
Comment #19
jeefers commentedI applied the patch and it seems to exclude and it puts things inline but i am having a problem. my output is showing up
Name
street city state zip
phone
and i need it to be
Name
Street
City state zip
phone
my inline is setup right was just wondering if for some reason it doesnt do a carriage return before the inline fields start.
Comment #20
letapjar commentedThe inline setting is a bit different than the normal views setting which is geared towards HTML output.
In this module the way it works is that fields marked as inline are all inlined - so if you have :
street
city
state
zip
and you want :
street
city state zip
you should only select the fields city, state and zip to be inlined - do not select "street" to be inlined.
in other words - the default output is to put a newline character in front of every field - when you check "inline" for any given field - that field is prepended with a space instead of a newline.
Comment #21
jeefers commentedyou lost me... When i get the preview it shows up properly.. (using the views inline settings)
Name
Street
City State Zip
Phone
When i print the pdf output i get:
Name
Street City State Zip
Phone
Is there another area where i need to set what is inline and what is not other than within the row style: fields section of views????
Comment #22
jeefers commentedbecause exclude is working..... instead of inline through views i can try doing the "rewrite the output of this field" through the the fields section of views to line everything up properly...??? Im just awaiting your thoughts....
Comment #23
letapjar commentedThis issue has nothing to do with excluding fields or rewriting the output of a field at all.
The patch as-is is working as designed on a number of installs I have so I am unable to reproduce your error.
Let's back up a minute. in #21 you said: " When i get the preview it shows up properly.. (using the views inline settings) "
Did you try simply unchecking "street" as a field to be inlined? - if not, please try that - it should give you the correct output on your labels - though your preview output will look incorrect.
The preview output is an HTML output that gets themed by views_theme(). The actual output of this module is a PDF file that does not use HTML at all. In views when HTML is generated, the way inlining is handled is to swap in a "span" element for a "div" element on inlined fields. This is not useful when formatting PDF labels.
Once you try the above (i.e. unchecking the inline property on the street field and seeing how the generated labels look) then if you really need the preview to match the label output, let's talk about that. To get the preview to match the actual output would require mailing_label handle it's own previews rather than letting views handle it. There are plenty of views plugins and attachements that do not preview properly - but where the actual output is correct. If there is a strong use case for having the preview being fixed I'm open to understanding it.
If your labels are STILL incorrect I'll need more info on how to reproduce the error.
Comment #24
jeefers commentedy wouldnt it work in my preview but work in the pdf doc...
i have street unchecked and it shows up properly in the preview and on the page before i hit Print Pdf button, but after the pdf document opens i get:
Name
Street City State Zip
Phone
Comment #25
jeefers commentedI tried rewriting the output and the output still displayed the excluded the items that were being rewritten. But other times were excluded as normal.
Comment #26
jeefers commentedNevermind... that was me being stupid.
Thanks,
Comment #27
jeefers commentedStill working with this Mailing Module... probably should have opened a new ticket but does anyone know how to adjust the spacing between lines... I would like the street name to be spaced farther away from the city, state and zip....
I adjust the private line height property but it doesnt work properly.
Comment #28
letapjar commentedThe spacing is not really manually configurable at this time. The problem is that the font size and positioning are automatically figured out by the ufpdf.php code depending upon what size label is selected. It would take quite a bit of re-working to allow users to manually adjust these parameters.
Comment #29
bluegeek9 commented