[英]Printing from web applications
如何从Web应用程序生成纸质打印?
具体来说,我正在考虑更复杂的纸质文件,如文凭,发票和合同。 包含框架,表格,徽标和页眉/页脚的可变页数。
今天我使用自定义表单和CSS用于某些事情,iTextSharp用于其他人(我使用asp.net和MS-SQL),但我认为这两种方法都非常耗时且很难在不同文档中保持一致。
还有更好的方法吗?
恕我直言,如果答案固定格式打印PDF
使用iTextSharp从头开始创建文档可能非常耗时。 作为替代方案,您可以通过向其添加表单字段来创建(或重用)PDF“模板”文档(如果需要)。 最简单的方法是使用完整版的Adobe Acrobat,但您也可以使用iTextSharp添加可填写的表单字段。
例如,对于奖励或文凭,您可以查找,创建或修改包含文档的所有文本,图形,花哨边框和字体的PDF,然后为收件人的名称添加表单字段。 您可以为日期,签名行,奖励类型等添加其他字段。
然后,您可以非常轻松地使用Web应用程序中的iTextSharp填写表单,将其展平并将其流回用户。
在这篇文章的最后是一个完整的ASHX处理程序代码示例。
还要记住,iTextSharp(或只是iText)对于组合来自不同文档的PDF文档或页面也很有用。 因此,对于具有封面或说明页面的固定设计但是动态生成内容的年度报告,您可以打开封面页,打开报告的模板页面,在模板的空白区域生成动态内容,打开后页“样板”,然后将它们全部合并到一个文档中以返回给用户。
using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Text;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace iTextFormFillerDemo
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class DemoForm : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/pdf";
//This line will force the user to either open or save the
//file instead of it appearing on its own page - if you remove it,
//the page will appear in the browser in the same window.
context.Response.AddHeader("Content-Disposition",
"attachment; filename=DemoForm_Filled.pdf");
FillForm(context.Response.OutputStream);
context.Response.End();
}
// Fills the form and pushes it out the output stream.
private void FillForm(System.IO.Stream outputStream)
{
//Need to get the proper directory (dynamic path) for this file.
//This is a filesystem reference, not a web/URL reference...
//The PDF reader reads in the fillable form
PdfReader reader = new PdfReader("C:/DemoForm_Fillable.pdf");
//The PDF stamper creates an editable working copy of the form from the reader
//and associates it with the response output stream
PdfStamper stamper = new PdfStamper(reader, outputStream);
//The PDF has a single "form" consisting of AcroFields
//Note that this is shorthand for writing out
//stamper.AcroFields.SetField(...) for each set
AcroFields form = stamper.AcroFields;
//Set each of the text fields this way: SetField(name, value)
form.SetField("txtFieldName", "Field Value");
form.SetField("txtAnotherFieldName", "AnotherField Value");
//Set the radio button fields using the names and string values:
form.SetField("rbRadioButtons", "Yes"); //or "No"
//Form flattening makes the form non-editable and saveable with the
//form data filled in
stamper.FormFlattening = true;
//Closing the stamper flushes it out the output stream
stamper.Close();
//We're done reading the file
reader.Close();
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
即使你已经说过你已经尝试过基于CSS的方法,但最好的方法是看一下@print媒体类型( http://www.w3.org/TR/CSS2/media.html ) 。 基本上允许您使用不同的样式表进行打印,而不是使用屏幕或任何其他(许多)媒体类型。
然后,如果要自动启动打印事件,则需要使用类似这样的内容(JavaScript):
window.print();
当然,您必须检查浏览器实际上是否支持通过JavaScript进行打印,并采取相应措施:
function doPrint() {
if(!window.print())
{
alert("Your browser does not support direct printing, please select 'Print' from the 'File' menu.");
}
}
但是,如果您坚持认为CSS无法满足您的需求,那么您可能需要采用基于PDF的解决方案。 虽然,我没有通过'PDF制作网页打印输出的经验。
如果您使用的是SQL Server,则可以使用报告服务创建报告。 然后,用户可以直接打印或将其自身导出为多种不同的格式(您可以控制用户可以使用的格式)。
我过去曾使用过这种方法,并且发现在打印或导出文档时,它可以让我更好地控制文档的外观。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.