-1
public FileResult Download() 
{ 

    var doc = new EO.Pdf.PdfDocument(); 
    EO.Pdf.HtmlToPdf.ConvertUrl("http://www.google.com/", doc); 

    var ms = new MemoryStream();     
    doc.Save(ms); 

    ms.Position = 0; 

    return new FileStreamResult(ms, "application/pdf") 
               { 
                   FileDownloadName = "download.pdf"
               }; 
}

如果可能的话,您能否展示一下如何扩展上面的代码以便能够将多个网页转换为一个 pdf 文档?

棘手的部分是我们不知道用户可能会尝试转换哪些页面。

因此,如上面的代码所示对网页进行硬编码对我们没有帮助。

任何帮助是极大的赞赏。

//Create a new PdfDocument object
var doc = new EO.Pdf.PdfDocument();

//Convert two ore more different pages into the same PdfDocument

EO.Pdf.HtmlToPdf.ConvertUrl("c:\\1.html", doc);

EO.Pdf.HtmlToPdf.ConvertUrl("c:\\2.html", doc);

最新代码:

public FileResult Download() 
{ 
    var doc = new EO.Pdf.PdfDocument(); 

foreach(var url in passedUrls)  
{ 
EO.Pdf.HtmlToPdf.ConvertUrl(url, doc);   
doc.Save(ms);   
}


    ms.Position = 0; 

    return new FileStreamResult(ms, "application/pdf") 
               { 
                   FileDownloadName = "download.pdf"
               }; 
}

亚当的最新消息(谢谢先生)

public FileResult Download() 
{ 

var documents = new List<EO.Pdf.PdfDocument>(); 
foreach(var url in passedUrls) 
{ 
    var doc = new EO.Pdf.PdfDocument(); 
    EO.Pdf.HtmlToPdf.ConvertUrl(url, doc); 
    documents.Add(doc); 
} 

EO.Pdf.PdfDocument mergedDocument = EO.Pdf.PdfDocument.Merge(documents.ToArray()); 
}

希望其他人发现这些代码很有用。

4

2 回答 2

2

根据帮助文档,我建议以下内容:

public FileResult Download()
{
    var urls = new List<string>
    { // Populate list with urls
        "C:\\1.html",
        "C:\\2.html"
    };

    var documents = new List<EO.Pdf.PdfDocument>();
    foreach(var url in urls)
    {
        var doc = new EO.Pdf.PdfDocument();
        EO.Pdf.HtmlToPdf.ConvertUrl(url, doc);
        documents.Add(doc);
    }

    EO.Pdf.PdfDocument mergedDocument = EO.Pdf.PdfDocument.Merge(documents.ToArray());

    var ms = new MemoryStream();
    mergedDocument.Save(ms);
    ms.Position = 0;

    return new FileStreamResult(ms, "application/pdf") { FileDownloadName = "download.pdf" };
}
于 2012-07-19T15:56:09.057 回答
1

将一组 url 字符串传递给函数

然后

foreach(var url in passedUrls) 
{
EO.Pdf.HtmlToPdf.ConvertUrl(url, doc);  
doc.Save(ms);  
}
于 2012-07-19T15:32:55.603 回答