首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Aspose将HTML转换为PDF

使用Aspose将HTML转换为PDF
EN

Stack Overflow用户
提问于 2016-04-15 00:45:08
回答 3查看 9.5K关注 0票数 2

我是Aspose的新手,但我已经成功地将几种文件格式转换为PDF格式,但我对HTML到PDF的转换感到震惊。我能够成功地将HTML文件转换为PDF,但CSS部分没有呈现到生成的PDF中。对此有什么想法吗?我将www.google.com保存为我的输入文件。这是我的控制器代码。

代码语言:javascript
复制
using Aspose.Pdf.Generator


Pdf pdf = new Pdf();
pdf.HtmlInfo.CharSet = "UTF-8";
Section section = pdf.Sections.Add();
StreamReader r = File.OpenText(@"Local HTML File Path");
Text text2 = new Aspose.Pdf.Generator.Text(section, r.ReadToEnd());
pdf.HtmlInfo.ExternalResourcesBasePath = "Local HTML File Path";
text2.IsHtmlTagSupported = true;
text2.IsFitToPage = true;
section.Paragraphs.Add(text2);
pdf.Save(@"Generated PDF File Path");

我是不是遗漏了什么?任何形式的帮助都是非常感谢的。

谢谢

EN

回答 3

Stack Overflow用户

发布于 2016-04-18 15:11:25

我的名字是Tilal Ahmad,我是Aspose的开发者布道者。

请使用新的DOM方法(Aspose.Pdf.Document)进行HTML到PDF的转换。在这种呈现外部资源(CSS/图像/字体)的方法中,需要将资源路径传递给HtmlLoadOptions()方法。有关此目的,请查看以下文档链接。

将HTML转换为DOM(新DOM)

代码语言:javascript
复制
HtmlLoadOptions options = new HtmlLoadOptions(resourcesPath);
Document pdfDocument = new Document(inputPath, options);
pdfDocument.Save("outputPath");

将网页转换为(新DOM)

代码语言:javascript
复制
// Create a request for the URL.
WebRequest request = WebRequest.Create("https:// En.wikipedia.org/wiki/Main_Page");
// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
// Time out in miliseconds before the request times out
// Request.Timeout = 100;

// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();

MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(responseFromServer));
HtmlLoadOptions options = new HtmlLoadOptions("https:// En.wikipedia.org/wiki/");


// Load HTML file
Document pdfDocument = new Document(stream, options);

options.PageInfo.IsLandscape = true;
// Save output as PDF format
pdfDocument.Save(outputPath);
票数 7
EN

Stack Overflow用户

发布于 2016-04-15 01:03:02

尝试在每个样式标记中使用媒体属性

代码语言:javascript
复制
<style media="print">

然后将html文件提供给您的Aspose.Pdf生成器。

票数 0
EN

Stack Overflow用户

发布于 2019-11-05 19:17:22

试试这个..。这对我来说很好

代码语言:javascript
复制
var license = new Aspose.Pdf.License();
license.SetLicense("Aspose.Pdf.lic");

var license = new Aspose.Html.License();
license.SetLicense("Aspose.Html.lic");

using (MemoryStream memoryStream = new MemoryStream())
{
      var options = new PdfRenderingOptions();
      using (PdfDevice pdfDevice = new PdfDevice(options, memoryStream))
      {
          using (var renderer = new HtmlRenderer())
          {
              using (HTMLDocument htmlDocument = new HTMLDocument(content, ""))
              {
                   renderer.Render(pdfDevice, htmlDocument);
                   //Save memoryStream into output pdf file
              }
           }
       }
 }

内容是字符串类型,这是我的html内容。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36629173

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档