2016-04-14 14 views
1

私はAsposeを初めて使っていますが、いくつかのファイルフォーマットをPDFに変換することに成功しましたが、HTMLからPDFへの変換に打撃を受けます。 HTMLファイルをPDFに変換することはできますが、CSS部分は生成されたPDFにレンダリングされません。これについてのアイデア? www.google.comを入力HTMLファイルとして保存しました。ここに私のコントローラコードです。Asposeを使用したHTMLからPDFへの変換

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"); 

私に何か不足していますか?どんな種類の助けも大歓迎です。

おかげ

答えて

0

は、各スタイルタグ

<style media="print"> 

のメディア属性を使用してみて、あなたのAspose.Pdfジェネレータにhtmlファイルを提供しています。

+0

おかげでそれは助けにはなりませんでした。 –

3

私の名前はTilal Ahmadで、私はAsposeの開発者エバンジェリストです。

HTMLからPDFへの変換に新しいDOMアプローチ(Aspose.Pdf.Document)を使用してください。外部リソース(CSS/Images/Fonts)をレンダリングするこのアプローチでは、リソースパスをHtmlLoadOptions()メソッドに渡す必要があります。その目的のために、以下のドキュメントリンクを確認してください。

改宗者PDFへのHTML(新しいDOM)

HtmlLoadOptions options = new HtmlLoadOptions(resourcesPath); 
Document pdfDocument = new Document(inputPath, options); 
pdfDocument.Save("outputPath"); 

PDFに変換するウェブページ(新しいDOM)ご返信用

// 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); 
+0

XHTMLをPDFに変換しますか? –

+1

@Mike、はい新しいDOMアプローチは、XHTMLからPDFへの対応をサポートしています。 HTML/HTMの代わりにXHTMLファイルを渡す必要があります。ファイルが外部リソースを使用している場合は、パスをHtmlLoadOptions()に渡します。 –

+0

@TilalAhmad:答えを更新してください。リンクが壊れています。 – Grevling

関連する問題