如何使用ASP.Net,C#中的abcpdf将HTML文件下载为PDF格式?使用abcpdf将pdf文件下载为PDF
3
A
回答
1
下ASP.NET例如,在C#中说明了如何从一个网页创建一个PDF和它传输到网络浏览器...
<% @Page Language="C#" %>
<% @Import Namespace="WebSupergoo.ABCpdf7" %>
<%
Doc theDoc = new Doc();
theDoc.AddImageUrl("http://www.example.com");
byte[] theData = theDoc.GetData();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "inline; filename=MyPDF.PDF");
Response.AddHeader("content-length", theData.Length.ToString());
Response.BinaryWrite(theData);
Response.End();
%>
从“内嵌”到“改变内容处置依恋'会改变行为。
有关Doc.GetData()函数的产品文档中还有一些需要注意的更多信息,您可能还会发现'Paged HTML Example'有帮助。
0
这就是你如何使用ABCPdf来实现这一目标。
http://www.websupergoo.com/helppdf7net/source/4-examples/13-pagedhtml.htm
Doc theDoc = new Doc();
theDoc.Rect.Inset(72, 144);
theDoc.Page = theDoc.AddPage();
int theID;
theID = theDoc.AddImageUrl("http://www.yahoo.com/");
while (true) {
theDoc.FrameRect(); // add a black border
if (!theDoc.Chainable(theID))
break;
theDoc.Page = theDoc.AddPage();
theID = theDoc.AddImageToChain(theID);
}
for (int i = 1; i <= theDoc.PageCount; i++) {
theDoc.PageNumber = i;
theDoc.Flatten();
}
theDoc.Save(Server.MapPath("pagedhtml.pdf"));
theDoc.Clear();
1
这种方法适用于我们的项目
/// <summary>
/// Converts Html to pdf
/// </summary>
/// <param name="htmlOrUrl">Html markup of html page URL</param>
/// <param name="isUrl">previous parameter is URL</param>
/// <param name="highQuality">use high quality converter engine</param>
/// <param name="indent">indent from all sides of the page</param>
/// <returns>Memory stream with PDF-file</returns>
public static MemoryStream HtmlToPDF(this String htmlOrUrl, Boolean isUrl, Boolean highQuality = false, Int32 indent = 20)
{
using (var doc = new Doc())
{
doc.Color.String = "0, 0, 0";
doc.HtmlOptions.UseScript = true;
doc.HtmlOptions.AddLinks = true;
if (highQuality)
{
doc.HtmlOptions.Engine = EngineType.Gecko;
}
// 1. CONTENT BLOCK
doc.Rect.Left = 0 + indent;
doc.Rect.Top = 792 - indent;
doc.Rect.Right = 612 - indent;
doc.Rect.Bottom = 0 + indent;
doc.AppendChainable(htmlOrUrl, isUrl);
var ms = new MemoryStream();
doc.Save(ms);
if (ms.CanSeek)
{
ms.Seek(0, SeekOrigin.Begin);
}
return ms;
}
}
/// <summary>
/// Appends document with multipage content
/// </summary>
private static void AppendChainable(this Doc doc, String htmlOrUrl, Boolean isUrl = false)
{
Int32 blockId = isUrl
? doc.AddImageUrl(htmlOrUrl)
: doc.AddImageHtml(String.Format(HtmlWrapper, htmlOrUrl));
while (doc.Chainable(blockId))
{
//doc.FrameRect(); // add a black border
doc.Page = doc.AddPage();
blockId = doc.AddImageToChain(blockId);
}
}
//使用
var testMs1 = ABCPdfConverter.ABCConverter.HtmlToPDF("https://developers.google.com
/chart/interactive/docs/examples", true, false, 20);
testMs1.StreamToFile(@"D:/3.pdf");
//和数据流以文件
/// <summary>
/// Saves stream instance to file
/// </summary>
public static void StreamToFile(this MemoryStream input, String outputFileName)
{
var dirName = Path.GetDirectoryName(outputFileName);
var fileName = Path.GetFileName(outputFileName);
if (String.IsNullOrEmpty(dirName) || String.IsNullOrWhiteSpace(fileName))
{
throw new IOException("outputFileName");
}
using (FileStream outStream = File.Create(outputFileName))
{
input.WriteTo(outStream);
outStream.Flush();
outStream.Close();
}
}
相关问题
- 1. ABCpdf损坏PDF文件
- 2. ABCPdf - 打开PDF文件
- 3. 用ABCPDF解析PDF
- 4. 用JavaScript下载PDF文件
- 5. 用C#下载PDF文件
- 6. Python - 下载pdf文件(非.pdf)url
- 7. 将html网页转换为pdf页面(用户将下载的pdf文件)
- 8. 使用php下载pdf文件
- 9. 如何使用asp.net下载pdf文件?
- 10. 使用chromedriver直接下载PDF文件
- 11. 使用webcrawler下载pdf文件
- 12. 使用OKHTTP下载pdf文件
- 13. 使用htaccess强制PDF文件下载
- 14. Alamofire不下载PDF文件
- 15. 使用wkhtmltopdf生成PDF并下载pdf
- 16. 下载页面为pdf文件
- 17. php pdf文件下载:无法加载PDF文档
- 18. HTML to PDF转换使用ABCpdf工具
- 19. 打印风景html-> pdf使用abcPDF
- 20. PHP:试图使pdf文件可下载
- 21. 在与angularjs相同的PDF文件夹中下载PDF文件
- 22. 从Zip文件中下载PDF文件
- 23. 使用tcpdf将mathml下载到PDF中
- 24. 使用Google Drive android SDK将文档下载为PDF
- 25. Ajax调用在jsp下载pdf文件
- 26. 使用PDFKit下载pdf
- 27. umbraco 7:从pdf下载附件pdf
- 28. 使用applescript将文件导出为pdf
- 29. 使用itextsharp下载zing图表为pdf
- 30. 如何使用SVG图形将PDF转换为PDF文件?