13

I am working on an ASP.Net web application that must print dynamically created labels on standard Avery-style label sheets (one particular size, so only one overall layout). The labels have a variable number of lines (3-6) and may contain either lines of text or a graphic barcode image.

Our first cut, that I inherited, used monospaced fonts to reduce the formatting issues, but that did not allow enough text to the fit on the labels and the customer was dissatisfied. Basically it was formatted text.

My next version used TABLEs, DIVs, CSS, and a bit of JavaScript calculations to format the labels using proportional fonts. It still required a bit of tweaking (the user had to set their print margins correctly and turn off the print headers and footers), but it seemed to work.

However, it seems that there are some variations on how different printers render the text (WYS ain't WYG), so even though we tested on different browsers using at least two different printers (an inkjet and a laser printer), some user's labels don't line up. Slight margin variations can be adjusted by adjusting the margins on the page setup dialog, but the harder problem is that the inter-label spacing can be off by a tiny fraction of an inch, so that if the first label is pretty well centered, by the end of the page the label text and images have crawled off the top or bottom of the labels.

We are about to the point of switching to generating Word, Excel, or PDF output which is going to take quite a bit of development time and possible add extra steps in the printing process.

So, does anyone have any suggestions on how to do an HTML/CSS layout that will precisely render on different types of printers? I don't really care if the line/word breaks are a bit different, but I need to be able to predictably position the upper left corners of each label area.

Right now the labels flow down the page in a table and we have been tweaking the box model of the cells and internal DIVs to make them a uniform height. I suspect that using absolute positioning of each element may be the best answer, but that is going to be tricky as well due to the ASP.Net generation of the label elements. If I knew for sure that would work, I would rather try it than throw away everything we have to go to a different generation method.

Slight Update: Right now I'm doing some tests with absolute positioning - setting only the top and left coordinate of a containing block element. So far there are minor variations on the offset onto the page (margins, paper alignment, etc.), but all browsers and printers tested put the elements in exactly the right spots relative to each other. I appreciate the PDF tips, but does anyone know of additional "gotchas" on using absolute positioning this way?

Update: For the record, I rewrote the label printing portion using iTextSharp and it works perfectly - definitely the way to do this in the future...

10 Answers 10

29

Forget HTML and make a PDF. HTML printing is extremely variable - not just across browsers but across different versions of the same browser. PDF is a lot easier.

Even if you get it exactly right with one browser / font setup / printer / phase of the moon, it will be the most fragile thing you've ever had to maintain. No matter how long you think it will take to make a PDF (and it's not really that hard as there are some free libraries out there), HTML will ultimately take a lot more of your time. PDF readers are widely deployed and print more consistently than even Word files.

3
  • 2
    I have to agree. We had a similar problem recently and generating PDF's was the quickest and simplest solution. We even went as far as providing a calibration page to handle label and print calibrations for different printers Oct 2, 2008 at 20:02
  • PDF would be more difficult, and less flexible, than a Word document, wouldn't it?
    – Matt Dawdy
    Oct 2, 2008 at 20:19
  • PDF is less flexible than a Word file in that you can't (easily) edit a PDF file. I don't think that's a consideration here though. PDF is definitely easier to generate than a Word file. This Wikipedia article lists some of the ways to do it: en.wikipedia.org/wiki/List_of_PDF_software
    – Neall
    Oct 2, 2008 at 20:34
5

The web is not a format that is guaranteed to get consistent print results. Given the standard support for label printing with MS Word, and the relative ease of automation and generation, I would strongly recommend going that route.

I'm not aware of ANY method to get percise printing across all types of browsers, operating systems, and printers when using web content.

1
  • 1
    You might be RIGHT NOW, but the next FireFox Update, the next IE update, and you rae back to square one. Oct 2, 2008 at 20:08
3

"precisely" and "printing" aren't two words that really work together that well. I did an OCR/OMR application a year or so ago, and even when building a PDF I saw significant differences between different print drivers and such. Because of that, my gut is to tell you that you might not have 100% success.

If CSS and layout issues don't work that well for you, you might need to resort to building the labels as images using GDI+ -- at least that way you can use GetFontMetrics() and such.

Good luck!

3

I had a similiar issue and the answer is you can't do it. Instead, I generated a PDF file in realtime using iTextSharp and passed that to the response.

2

Using SQL Server Reporting Services, I generate a PDF to send to the printer, but it can be seen as HTML on the screen using the control you can include in your web pages. There are RDLC files that are available on the internet to print to various Avery formats.

2

I rewrote the SharpPDFLabel code that was mentioned back in 2011 this week as I needed it to be a lot more flexible (and to work with the current iTextSharp library).

You can get it here: https://github.com/finalcut/SharpPDFLabel

I added the ability to specify the contents of each individual label if you want (or to continue creating a sheet of identical labels too). By extending the LabelDefinition class you can specify the layout of your labels pretty easily.

1
  • off the top of my head I'm not sure sorry. I haven't used this library much since I made my update. I didn't need to add barcodes to any of mine; Just names and addresses (mailing labels). I assume it is an image (the barcode) so maybe something like: cs singleSheetLabelCreator.AddImage(myImageAsAStream); Jun 2, 2017 at 14:44
1

I also struggled with the HTML/CSS approach due to the inconsistent printing behaviour across browsers.

I created a C# library to produce Avery Labels from ASP.NET which I hope you might find useful:

https://github.com/wheelibin/SharpPDFLabel#readme

You can add images and text to the labels, and it's easy to define more labels types.

(I use it for barcode labels, the barcode is generated as an image and then added to the label using this library.)

Cheers

3
  • I am only downvoting this because the code is now outdated. I did use your project as the basis for my answer though so I feel bad/awkward for downvoting you. Nov 13, 2015 at 14:42
  • 1
    haha, don't worry about it - glad it was of some use to someone (might have been nice if you had contributed back to the original to make it better instead of just forking it, but hey ho)
    – wheelibin
    Nov 13, 2015 at 15:38
  • I had assumed, incorrectly it seems, that you weren't really maintaining the old project so I didn't want to bug you about it. I've sent you a pull request. Nov 16, 2015 at 16:52
0

Add a few options to your app that let users adjust spacing for their particular configuration. You could include this right on the label if you want, and style it away via media selectors, but you'll probably want to persist them somewhere, too.

1
  • We can't get them to set their margins and turn of their headers and footers reliably and the instructions for that are displayed right on the label web page (and hidden by media selectors on print), so asking them to make micro-adjustments to the inter-label spacing won't work for us.
    – CMPalmer
    Oct 2, 2008 at 19:57
0

Flash is also good method to push a printable like a label albeit a little more complex to implement and maintain. In most cases it displays much quicker than a PDF and you can embed it into the design of the page and simply add a "Print" button within the flash.

I did this several years ago when we were using HTML and PDF to generate confirmation receipts. HTML is "ok" but is at the mercy of the end users web browser so we quickly dumped that method. PDF's are good as long as they have a PDF reader, which to our surprise a lot of our customers did not. So that was dumped as well after we switched to a FLASH version using a simple flash movie that included a few dynamic text areas and a "print" button. I communicated the data between the page and flash using a few flash vars. You can also use web service.

When I need something more than just simple text I use the free community edition of the PDF Generator component from DynamicPDF.com. It works great and is very quick.

0

I just went through the same thing. Ended up switching and making a short little JSF app (running on Glassfish) that uses JasperReports to print directly to the lable printer. Push button, instant label at the printer, don't even have to view it on-screen if you don't want to since Jasper can directly output to printer (as well as PDF in browser).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.