转换事业部内容为PDF格式,节省服务器的路径上 [英] Convert Div Content to PDF and save on server path

查看:127
本文介绍了转换事业部内容为PDF格式,节省服务器的路径上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要事业部内容转换成PDF格式,节省服务器路径上创建PDF文件没有要求我保存的文件(下载文件)。

I want to convert Div content into pdf and save that created pdf file on server path without asking to me save file (download file).

我转换的PDF文件,并​​保存ASLO该文件在服务器路径。

I converted pdf file and aslo saved that file in server path.

但在我的code这也说明,我想删除文件下载选项。

But In my code it also shows file download option which I want to remove.

我不希望给PDF文件另存为的选择,因为我救了创建服务器路径文件。

I don't want to give pdf file save as option because I saved created file in server path.

下面是我的code:

        string appPath = HttpContext.Current.Request.ApplicationPath;
        string path = Server.MapPath(appPath + "/Attachment/Test.pdf");
        if (File.Exists(path))
            File.Delete(path);
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        createDiv.RenderControl(hw);
        var output = new FileStream(path, FileMode.Create);
        StringReader sr = new StringReader(sw.ToString());
        iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(PageSize.A4, 10f, 10f, 100f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc, output);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.End();

谢谢,
亚太区首席技术官Matt

Thanks, Hitesh

推荐答案

究其原因,保存对话框中显示是因为你写的响应。如果你只是想保存到本地磁盘,只需删除所有引用的回应。然后,你可以做一个的Response.Redirect(...)或类似的用户指向下一个页面。

The reason the save dialog is showing is because you are writing to the response. If you only want to save to the local disk, simply remove all references to response. You can then do a Response.Redirect(...) or similar to point the user to the next page.

使用的MemoryStream

如果你打算到PDF写入数据库,你应该使用的 的MemoryStream (参见MSDN)代替的FileStream 。然后,您可以调用 MemoryStream.ToArray() 方法,它会返回一个字节数组,你可以存储在数据库中。

If you intend to write the PDF to a database you should use a MemoryStream (see MSDN) instead of a FileStream. You can then call the MemoryStream.ToArray() method which will return a byte array you can store in the database.

假设你正在使用实体框架:

Assuming you are using Entity Framework:

var output = new MemoryStream();
//snip
iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc, output);
//snip
myEntityFrameworkObject.Data = output.ToArray();
DataContext.SaveChanges();

这篇关于转换事业部内容为PDF格式,节省服务器的路径上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆