Skip to content

Commit 21fcc38

Browse files
author
ArthurHub
committed
HtmlRender.PdfSharp creates pdfs!
1 parent 79003a3 commit 21fcc38

10 files changed

Lines changed: 319 additions & 11 deletions

File tree

Source/Demo/WinForms/HtmlRenderer.Demo.WinForms.csproj

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@
6060
<ApplicationIcon>html.ico</ApplicationIcon>
6161
</PropertyGroup>
6262
<ItemGroup>
63+
<Reference Include="PdfSharp">
64+
<HintPath>..\..\packages\PDFsharp.1.32.3057.0\lib\net20\PdfSharp.dll</HintPath>
65+
</Reference>
66+
<Reference Include="PdfSharp.Charting">
67+
<HintPath>..\..\packages\PDFsharp.1.32.3057.0\lib\net20\PdfSharp.Charting.dll</HintPath>
68+
</Reference>
6369
<Reference Include="System" />
6470
<Reference Include="System.Data" />
6571
<Reference Include="System.Deployment" />
@@ -90,6 +96,11 @@
9096
<SubType>Designer</SubType>
9197
<DependentUpon>DemoForm.cs</DependentUpon>
9298
</EmbeddedResource>
99+
<Compile Include="Resource1.Designer.cs">
100+
<AutoGen>True</AutoGen>
101+
<DesignTime>True</DesignTime>
102+
<DependentUpon>Resource1.resx</DependentUpon>
103+
</Compile>
93104
<Compile Include="SampleForm.cs">
94105
<SubType>Form</SubType>
95106
</Compile>
@@ -98,6 +109,10 @@
98109
</Compile>
99110
</ItemGroup>
100111
<ItemGroup>
112+
<EmbeddedResource Include="Resource1.resx">
113+
<Generator>ResXFileCodeGenerator</Generator>
114+
<LastGenOutput>Resource1.Designer.cs</LastGenOutput>
115+
</EmbeddedResource>
101116
<EmbeddedResource Include="SampleForm.resx">
102117
<SubType>Designer</SubType>
103118
<DependentUpon>SampleForm.cs</DependentUpon>
@@ -121,6 +136,10 @@
121136
</BootstrapperPackage>
122137
</ItemGroup>
123138
<ItemGroup>
139+
<ProjectReference Include="..\..\HtmlRenderer.PdfSharp\HtmlRenderer.PdfSharp.csproj">
140+
<Project>{ca249f5d-9285-40a6-b217-5889ef79fd7e}</Project>
141+
<Name>HtmlRenderer.PdfSharp</Name>
142+
</ProjectReference>
124143
<ProjectReference Include="..\..\HtmlRenderer.WinForms\HtmlRenderer.WinForms.csproj">
125144
<Project>{1b058920-24b4-4140-8ae7-c8c6c38ca52d}</Project>
126145
<Name>HtmlRenderer.WinForms</Name>
@@ -137,6 +156,9 @@
137156
<ItemGroup>
138157
<EmbeddedResource Include="html.ico" />
139158
</ItemGroup>
159+
<ItemGroup>
160+
<None Include="packages.config" />
161+
</ItemGroup>
140162
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
141163
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
142164
Other similar extension points exist, see Microsoft.Common.targets.

Source/Demo/WinForms/Program.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
using System;
1414
using System.Windows.Forms;
15+
using HtmlRenderer.PdfSharp;
16+
using PdfSharp;
1517

1618
namespace HtmlRenderer.Demo.WinForms
1719
{
@@ -23,6 +25,14 @@ internal static class Program
2325
[STAThread]
2426
private static void Main()
2527
{
28+
// string html = Resource1.String1;
29+
// string html = "a<p style='background-color: yellow'>hello <b>world</b></p>";
30+
31+
// var doc = PdfGenerator.GeneratePdf(html, PageSize.A4);
32+
33+
// doc.Save(@"d:\doc.pdf");
34+
// return;
35+
2636
Application.EnableVisualStyles();
2737
Application.SetCompatibleTextRenderingDefault(false);
2838
Application.Run(new DemoForm());

Source/Demo/WinForms/Resource1.Designer.cs

Lines changed: 81 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<root>
3+
<!--
4+
Microsoft ResX Schema
5+
6+
Version 2.0
7+
8+
The primary goals of this format is to allow a simple XML format
9+
that is mostly human readable. The generation and parsing of the
10+
various data types are done through the TypeConverter classes
11+
associated with the data types.
12+
13+
Example:
14+
15+
... ado.net/XML headers & schema ...
16+
<resheader name="resmimetype">text/microsoft-resx</resheader>
17+
<resheader name="version">2.0</resheader>
18+
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
19+
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
20+
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
21+
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
22+
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
23+
<value>[base64 mime encoded serialized .NET Framework object]</value>
24+
</data>
25+
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
26+
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
27+
<comment>This is a comment</comment>
28+
</data>
29+
30+
There are any number of "resheader" rows that contain simple
31+
name/value pairs.
32+
33+
Each data row contains a name, and value. The row also contains a
34+
type or mimetype. Type corresponds to a .NET class that support
35+
text/value conversion through the TypeConverter architecture.
36+
Classes that don't support this are serialized and stored with the
37+
mimetype set.
38+
39+
The mimetype is used for serialized objects, and tells the
40+
ResXResourceReader how to depersist the object. This is currently not
41+
extensible. For a given mimetype the value must be set accordingly:
42+
43+
Note - application/x-microsoft.net.object.binary.base64 is the format
44+
that the ResXResourceWriter will generate, however the reader can
45+
read any of the formats listed below.
46+
47+
mimetype: application/x-microsoft.net.object.binary.base64
48+
value : The object must be serialized with
49+
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
50+
: and then encoded with base64 encoding.
51+
52+
mimetype: application/x-microsoft.net.object.soap.base64
53+
value : The object must be serialized with
54+
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
55+
: and then encoded with base64 encoding.
56+
57+
mimetype: application/x-microsoft.net.object.bytearray.base64
58+
value : The object must be serialized into a byte array
59+
: using a System.ComponentModel.TypeConverter
60+
: and then encoded with base64 encoding.
61+
-->
62+
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
63+
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
64+
<xsd:element name="root" msdata:IsDataSet="true">
65+
<xsd:complexType>
66+
<xsd:choice maxOccurs="unbounded">
67+
<xsd:element name="metadata">
68+
<xsd:complexType>
69+
<xsd:sequence>
70+
<xsd:element name="value" type="xsd:string" minOccurs="0" />
71+
</xsd:sequence>
72+
<xsd:attribute name="name" use="required" type="xsd:string" />
73+
<xsd:attribute name="type" type="xsd:string" />
74+
<xsd:attribute name="mimetype" type="xsd:string" />
75+
<xsd:attribute ref="xml:space" />
76+
</xsd:complexType>
77+
</xsd:element>
78+
<xsd:element name="assembly">
79+
<xsd:complexType>
80+
<xsd:attribute name="alias" type="xsd:string" />
81+
<xsd:attribute name="name" type="xsd:string" />
82+
</xsd:complexType>
83+
</xsd:element>
84+
<xsd:element name="data">
85+
<xsd:complexType>
86+
<xsd:sequence>
87+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
88+
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
89+
</xsd:sequence>
90+
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
91+
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
92+
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
93+
<xsd:attribute ref="xml:space" />
94+
</xsd:complexType>
95+
</xsd:element>
96+
<xsd:element name="resheader">
97+
<xsd:complexType>
98+
<xsd:sequence>
99+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
100+
</xsd:sequence>
101+
<xsd:attribute name="name" type="xsd:string" use="required" />
102+
</xsd:complexType>
103+
</xsd:element>
104+
</xsd:choice>
105+
</xsd:complexType>
106+
</xsd:element>
107+
</xsd:schema>
108+
<resheader name="resmimetype">
109+
<value>text/microsoft-resx</value>
110+
</resheader>
111+
<resheader name="version">
112+
<value>2.0</value>
113+
</resheader>
114+
<resheader name="reader">
115+
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
116+
</resheader>
117+
<resheader name="writer">
118+
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119+
</resheader>
120+
<data name="String1" xml:space="preserve">
121+
<value>&lt;body style="font: 10pt Tahoma"&gt;
122+
&lt;h3 style="color: navy; margin-bottom: 8px"&gt;Render to Image&lt;/h3&gt;
123+
&lt;hr /&gt;
124+
&lt;table style="border: 1px solid maroon; margin-top: 5px"&gt;
125+
&lt;tr style="vertical-align: top;"&gt;
126+
&lt;td width="32" style="padding: 2px 0 0 0"&gt;
127+
&lt;img src="http://download-codeplex.sec.s-msft.com/Download?ProjectName=HtmlRenderer&amp;DownloadId=770243" /&gt;
128+
&lt;/td&gt;
129+
&lt;td&gt;This &lt;i&gt;text&lt;/i&gt; is inside a &lt;b&gt;table&lt;/b&gt; &lt;u&gt;element&lt;/u&gt;.&lt;br /&gt;
130+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur ornare mollis elit.
131+
&lt;/td&gt;
132+
&lt;/tr&gt;
133+
&lt;/table&gt;
134+
&lt;ul style="margin-top: 5px"&gt;
135+
&lt;li&gt;&lt;span style="color: red"&gt;Colors&lt;/span&gt;&lt;/li&gt;
136+
&lt;li&gt;&lt;span style="background-color: #8dd"&gt;Back colors&lt;/span&gt;&lt;/li&gt;
137+
&lt;/ul&gt;
138+
&lt;/body&gt;</value>
139+
</data>
140+
</root>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="PDFsharp" version="1.32.3057.0" targetFramework="net40-Client" />
4+
</packages>

Source/HtmlRenderer.PdfSharp/Adapters/GraphicsAdapter.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,22 @@ internal sealed class GraphicsAdapter : RGraphics
3838
/// </summary>
3939
private readonly bool _releaseGraphics;
4040

41+
/// <summary>
42+
/// Used to measure and draw strings
43+
/// </summary>
44+
private static readonly XStringFormat _stringFormat;
45+
4146
#endregion
4247

4348

49+
static GraphicsAdapter()
50+
{
51+
_stringFormat = new XStringFormat();
52+
_stringFormat.Alignment = XStringAlignment.Near;
53+
_stringFormat.LineAlignment = XLineAlignment.Near;
54+
_stringFormat.FormatFlags = XStringFormatFlags.MeasureTrailingSpaces;
55+
}
56+
4457
/// <summary>
4558
/// Init.
4659
/// </summary>
@@ -90,7 +103,7 @@ public override RSize MeasureString(string str, RFont font)
90103
{
91104
var fontAdapter = (FontAdapter)font;
92105
var realFont = fontAdapter.Font;
93-
var size = _g.MeasureString(str, realFont);
106+
var size = _g.MeasureString(str, realFont, _stringFormat);
94107

95108
if (font.Height < 0)
96109
{
@@ -109,8 +122,8 @@ public override RSize MeasureString(string str, RFont font, double maxWidth, out
109122

110123
public override void DrawString(string str, RFont font, RColor color, RPoint point, RSize size, bool rtl)
111124
{
112-
var brush = new XSolidBrush(Utils.Convert(color));
113-
_g.DrawString(str, ((FontAdapter)font).Font, brush, point.X - font.LeftPadding * .8f, point.Y);
125+
var xBrush = ((BrushAdapter)_adapter.GetSolidBrush(color)).Brush;
126+
_g.DrawString(str, ((FontAdapter)font).Font, xBrush, point.X, point.Y, _stringFormat);
114127
}
115128

116129
public override RBrush GetLinearGradientBrush(RRect rect, RColor color1, RColor color2, double angle)

Source/HtmlRenderer.PdfSharp/Adapters/PdfSharpAdapter.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,22 @@ protected override RColor GetColorInt(string colorName)
6464

6565
protected override RPen CreatePen(RColor color)
6666
{
67-
return null;
67+
return new PenAdapter(new XPen(Utils.Convert(color)));
6868
}
6969

7070
protected override RBrush CreateSolidBrush(RColor color)
7171
{
72-
return null;
72+
XBrush solidBrush;
73+
if (color == RColor.White)
74+
solidBrush = XBrushes.White;
75+
else if (color == RColor.Black)
76+
solidBrush = XBrushes.Black;
77+
else if (color.A < 1)
78+
solidBrush = XBrushes.Transparent;
79+
else
80+
solidBrush = new XSolidBrush(Utils.Convert(color));
81+
82+
return new BrushAdapter(solidBrush);
7383
}
7484

7585
protected override RImage ConvertImageInt(object image)

Source/HtmlRenderer.PdfSharp/HtmlContainer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public sealed class HtmlContainer : IDisposable
4242
public HtmlContainer()
4343
{
4444
_htmlContainerInt = new HtmlContainerInt(PdfSharpAdapter.Instance);
45+
_htmlContainerInt.AvoidAsyncImagesLoading = true;
46+
_htmlContainerInt.AvoidImagesLateLoading = true;
4547
}
4648

4749
/// <summary>

0 commit comments

Comments
 (0)