感谢您共享请求的数据。
从下载Roboto font后,我们修改了HTML文件
Google Fonts
。请确保HTML文件在浏览器中打开时以各自的字体显示内容。您可以根据需要修改HTML。下面的代码示例通过在浏览器中打开HTML文件,将HTML转换为PDF文件,同时呈现与显示内容完全相同的内容。您可以从下载源字体文件和生成的PDF文档
this Google Drive link
。
dataDir = dataDir + "Roboto\\";
String SimpleStyledFilePath = dataDir + "FirstFile_Aspose.HTML.html";
using (FileStream fs = File.Create(SimpleStyledFilePath))
using (StreamWriter sw = new StreamWriter(fs))
{
sw.Write(
@"<style>
@font-face { font-family: Roboto; src: url('Roboto-Bold.ttf'); }
body {
font-family: 'Roboto';
}
.st
{
color: green;
}
</style>
<div id=id1>Aspose.Html rendering Text in Black Color</div>
<div id=id2 class=''st''>Aspose.Html rendering Text in Green Color</div>
<div id=id3 class=''st'' style='color: blue;'>Aspose.Html rendering Text in Blue Color</div>
<div id=id3 class=''st'' style='color: red;'><font face='Arial'>Aspose.Html rendering Text in Red Color</font></div>
");
}
string pdf_output;
// Create HtmlRenderer object
using (Aspose.Html.Rendering.HtmlRenderer pdf_renderer = new Aspose.Html.Rendering.HtmlRenderer())
// Create HtmlDocument instnace while passing path of already created HTML file
using (Aspose.Html.HTMLDocument html_document = new Aspose.Html.HTMLDocument(SimpleStyledFilePath))
{
// Set the page size less than document min-width. The content in the resulting file will be cropped becuase of element with width: 200px
Aspose.Html.Rendering.Pdf.PdfRenderingOptions pdf_options = new Aspose.Html.Rendering.Pdf.PdfRenderingOptions
{
PageSetup =
{
AnyPage = new Aspose.Html.Drawing.Page(new Aspose.Html.Drawing.Size(100, 100)), AdjustToWidestPage = false
},
};
pdf_output = dataDir + "not-adjusted-to-widest-page_out.pdf";
using (Aspose.Html.Rendering.Pdf.PdfDevice device = new Aspose.Html.Rendering.Pdf.PdfDevice(pdf_options, pdf_output))
{
// Render the output
pdf_renderer.Render(device, html_document);
}
// Set the page size less than document min-width and enable AdjustToWidestPage option. The page size of the resulting file will be changed according to content width
pdf_options = new Aspose.Html.Rendering.Pdf.PdfRenderingOptions
{
PageSetup =
{
AnyPage = new Aspose.Html.Drawing.Page(new Aspose.Html.Drawing.Size(100, 100)), AdjustToWidestPage = true
},
};
pdf_output = dataDir + "adjusted-to-widest-page_out.pdf";
using (Aspose.Html.Rendering.Pdf.PdfDevice device = new Aspose.Html.Rendering.Pdf.PdfDevice(pdf_options, pdf_output))
{
// Render the output
pdf_renderer.Render(device, html_document);
}
}
通过在任何浏览器中打开HTML文件,可以将生成的PDF文件与HTML文件进行比较。如果您注意到这两个文件之间有任何差异,请随时与我们联系。我们很乐意帮忙。