Printing Avery labels from ASP.NET MVC
I'm trying to dynamically generate and print Avery labels from an ASP.NET MVC 2 application. The labels will be limited to about 5 or so different templates. I am wondering what the best way for doing this is? The Labels are critical to our application and as such, we need to reduce printing error as much as possible.
I have heard I can go for the kludgey approach and make HTML templates, however there will be numerous printing errors. The better approach I've come across is to print 开发者_运维知识库to PDF. Are there any pitfalls for this approach and how difficult is it to implement? Also would I go about doing this? Does anyone know of any API's or third party software that would take care of this functionality?
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 in PDF 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
Take a look as CSS Media options. http://www.w3.org/TR/CSS2/media.html
Basically you can target styling to the printer, which can hide all the non essential guff on the page and perform fine tuned layout options.
-- Update: More info on PDF generation:
With PDF's typically you have a PDF engine (and there are a LOT out there) on the server with a template that generates a PDF file, this file is then usually downloaded to the browser via a generic handler. Generating your PDF with an html template on the server should eliminate your browser inconsistencies.
http://www.codeproject.com/KB/aspnet/Creating_PDF_documents_in.aspx
I stumbled on this technique that uses the Razor View Engine to generate PDF's ala MVC, looks promosing.
http://www.emphess.net/2010/11/09/create-high-quality-pdfs-with-razor-view-engine-and-latex/
Wheelibin's SharpPDFLabel is good if you want to print the same content multiple times on your sheet. If you need to print non-identical labels (i.e. each label on the sheet has different content), I blogged my MVC / iTextSharp / PDF solution here.
I would use iTextSharp to create a PDF file containing the required format.
It is a pretty popular open source PDF library for use in .Net. http://sourceforge.net/projects/itextsharp/
If this is an intranet application and a small number of concurrent users, I will recommend you to consider using Word Automation. Over the years I have implemented Win32 Apps, Winform and Web Apps that automate Ms. Word and although many will critize the use of it, I personally find that it has its niche and can be very useful and work well if managed correctly. For printing to label, some of the key concerns will be layout, formatting and paging. The beauty of using Word MailMerge, you can design and test all that using Ms. Word. You can then create a template file and from the automation perspective, all you need to do is:
- Open the specific template file (from your 5 templates)
- Specify your data source
- Specify your mail merge option
- Execute the mail merge
- Print the resulting document to printer or save it to PDF (directly if you are using 2007 or later, or print to 3rd party pdf driver)
- Close the document & word app
Another very nice feature of using word automation is your ability to record macro and look into the code. So for example, if you are not sure on how to set the data source/mail merge option above, you can record the macro as you are doing that in the UI. Later, look into the generated VBA code and transpose the same logic in your .NET code to achieve the same processing logic flow.
Here is some info to get you started (based on Word 2003, but the object model is very much the same for newer version) http://www.c-sharpcorner.com/UploadFile/amrish_deep/WordAutomation05102007223934PM/WordAutomation.aspx
In relation to your question about issues in creating PDF output. The one thing that often gets us is when we use barcodes. When we do this and generate a PDF, users sometimes print with settings like "scale to fit page" turned on which could resize the output and cause the barcodes to be unreadable.
I believe using the @print css media type declarations will be the best overall solution for "imprinting" labels. Experimentation will be required as various printers and layout details could affect the layout but I'm working on a solution which requires this now and am going primarily in that direction. I'll do my best to follow up and let you know how well it works. I need to set our online application to imprint name badges on sheets of Avery 5392 6-up perforated badges.
One idea that comes to mind is to use a CSS style like this one, and also a library to convert HTML to PDF on the server, so that output is consistent for all clients. I use EvoPDF for the latter; this costs money but there are free options as well.
This can be achieved by manipulating the XML in an avery-formatted .DOCX file. No third party components required, and you can modify it to suit any datasource, XML, DataTable, etc.
For information on this, see my blog article "Printable Avery Labels via ASP.net and Microsoft Word XML".
精彩评论