Quantcast
Channel: PDF tips & tricks
Viewing all articles
Browse latest Browse all 125

Converting html to pdf using markup parser (C# sample)

$
0
0

Introduction


Sometimes, you have a pre-built HTML fragment and would like to convert it to PDF with minimal changes in its appearance. You could manually split it down to atomic elements like spans, DIVs et cetera, create corresponding pdf objects and get the job done. However, it may become a time consuming and boring task which could be easily automated using Apitron PDF Kit for .NETcomponent. Strictly speaking, it’s not an HTML to PDF converter in its traditional meaning, but it provides you with an API to parse any custom markup (provided it’s XML-based) into pdf, and HTML is just the one of the possible markups.

Content wrapped by a tag will be converted to a flow layout content element with its Class property set to the tag’s name. You can define appropriate styles, and style these elements using class selectors. Images can be added using reserved <img>tag. Attributes link and bookmark are reserved to produce internal or external links and bookmarks. Idattribute can be used to assign an Id to the element. While it all may sound a bit complicated it’s actually pretty simple and convenient. Let’s see the code sample from the next section.

If you’d like to read more about custom markup parsing, we have a free book for you - Apitron PDF Kit in action.

Basic HTML to PDF conversion


Source HTML


Consider the following html fragment:

...
<imgsrc='myimage.jpg'height='48px'width='48px'style='float: left;'/>
Apitron PDF Kit for .NET component can be used to convert html fragments to PDF.
<br/>
Here you can see <b>bold</b> or <i>italic</i> text elements. Next sentence contains a hyperlink.
<br/>
Read the complete description of library's capabilities on the <ahref='www.apitron.com/product/pdf-kit'>product page</a>
...


It has bold and italic text, image element, and a link to other web page. All these elements will be converted to content elements defined in flow layout API subset and styled according to defined styles. You may also note the use of additional <br> element - it adds a line break.

In web browser it looks like this:

Pic. 1 HTML fragment sample pdf

Pic. 1 HTML fragment sample

Conversion implementation


The following code performs the actual conversion:

publicvoid ConvertHtmlFragmentToPDF()
{
    // prepare resources
    ResourceManager resourceManager = newResourceManager();   

    // create and fill document
    FlowDocument doc = newFlowDocument() { Margin = newThickness(5, 5, 5, 5) };
   
    // register styles for different elements
    doc.StyleManager.RegisterStyle(".img", newStyle(){Float = Float.Left});
    doc.StyleManager.RegisterStyle(".b",newStyle()
    {Font=newFont(StandardFonts.HelveticaBold,12)});
    doc.StyleManager.RegisterStyle(".i",newStyle()
    {Font=newFont(StandardFonts.HelveticaOblique,12)});
    doc.StyleManager.RegisterStyle(".a", newStyle(){Color=RgbColors.Blue,
    TextDecoration = newTextDecoration(TextDecorationOptions.Underline)});   

    // parse html markup
    doc.AddItems(ContentElement.FromMarkup(
    "<img src='myimage.jpg' height='48px' width='48px'"+
    "style='float: left;'/> Apitron PDF Kit for .NET component can be used to convert html"+
    " fragments to PDF.<br/> Here you can see <b>bold</b> or <i>italic</i> text elements. "+
    "Next sentence contains a hyperlink.<br/>Read the complete description of library's"+
    " capabilities on the <a href='www.apitron.com/product/pdf-kit'>product page</a>."));

    // save document
    using (Stream stream = File.Create("ConvertHtmlFragmentToPDF.pdf"))
    {
        doc.Write(stream, resourceManager);
    }
}


Results of the conversion are as follows:

Pic. 2 Markup parsing results pdf
Pic. 2 Markup parsing results

Conclusion


This post demonstrates how to create basic html to pdf converter and how to work with markup parsing API provided by Apitron PDF Kit. Create apps using your favorite .NET programming language: C#, VB .NET or any other. This library is available for many modern platforms, and you’ll most likely find yours supported. Check out the samples included in download package and let us know if you have any questions or comments.

Viewing all articles
Browse latest Browse all 125

Trending Articles