This is a migrated thread and some comments may be shown as answers.

There a way for PDF Export Formatting?

4 Answers 117 Views
Editor
This is a migrated thread and some comments may be shown as answers.
Fred
Top achievements
Rank 1
Fred asked on 26 Jan 2009, 08:00 PM
There a way to Format the PDF that you export from RadEditor?  It doesn't seem to carry over any HTML formatting.  Also, looking for documentation on the Export Settings, not sure how best to use that and if that would solve this problem.

Formatting like Underline and Italics work but not bold, or superscript or <h1>Title</h1> type stuff as well.


4 Answers, 1 is accepted

Sort by
0
Rumen
Telerik team
answered on 29 Jan 2009, 02:36 PM
Hi Fred,

The export to pdf functionality is still in development and it does not render some tags.

We do not have a detailed list with the supported and non supported tags, but here is some important information:

- for each TD element you should add a COL element inside the TABLE, e.g.

   <table width="100px">
        <colgroup>
            <col />
        </colgroup>
        <tbody>
            <tr>
                <td>
                    TEST
                </td>
            </tr>
        </tbody>
    </table>


- the <strong> and <b> tags are not supported and to apply bold formatting you should set style="font-weight:bold;", e.g.

<span style="font-weight:bold;">test</span>

Regards,
Rumen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Eirik H
Top achievements
Rank 2
answered on 14 May 2010, 09:32 AM

For anyone with the same challenge, I've solved this by using Html Agility Pack. The PdfFilter() function also adds colgroups and cols, as needed when exporting tables ..

Before exporting the content of editor.Content:

Public Class PdfUtil
        Public Shared Function PdfFilter(ByVal html As StringByVal applyFormatting As BooleanAs String
            Dim htmlDoc As HtmlDocument = New HtmlDocument()
            htmlDoc.OptionOutputOriginalCase = True
            htmlDoc.OptionWriteEmptyNodes = True
            htmlDoc.LoadHtml(PdfUtil.StripStyles(html))
            If htmlDoc.ParseErrors IsNot Nothing AndAlso htmlDoc.ParseErrors.Count > 0 Then
                ' Could not parse html.. TODO: alert user? or just ... 
                Return html ' :)
            Else
                If htmlDoc.DocumentNode IsNot Nothing Then
                    Dim tables As HtmlNodeCollection = htmlDoc.DocumentNode.SelectNodes("//table")
                    If (tables IsNot Nothing AndAlso tables.Count() > 0) Then
                        For Each table As HtmlNode In tables
                            ' Tables must have width for the export:
                            table.SetAttributeValue("style""width:100%;")
                            ' Tables must have colgroup for the export:
                            Dim colGroups As HtmlNodeCollection = table.SelectNodes("//colgroup")
                            If (colGroups IsNot Nothing AndAlso colGroups.Count() > 0) Then
                                ' If there are colgroups - delete them and create new ones (sometimes the number of cols doesn't match the number of td's)
                                For Each colGroup As HtmlNode In colGroups
                                    colGroup.Remove()
                                Next
                            End If
                            ' Create new colgroup
                            Dim newColGroup As HtmlNode = htmlDoc.CreateElement("colgroup")
                            ' Create cols
                            Dim numRows As Integer = table.SelectSingleNode("//tr").SelectNodes("th|td").Count()
                            Dim newCol As HtmlNode
                            For index As Integer = 1 To numRows
                                newCol = htmlDoc.CreateElement("col")
                                newColGroup.AppendChild(newCol)
                            Next
                            table.InsertBefore(newColGroup, table.FirstChild)
                        Next
                    End If
                    If (applyFormatting) Then
                        PdfUtil.ApplyFormatting(htmlDoc.DocumentNode)
                    End If
                End If
            End If
            Dim sb As New StringBuilder()
            Dim sw As New IO.StringWriter(sb)
            htmlDoc.Save(sw)
            sw.Flush()
            Return sw.ToString()
        End Function
        Public Shared Sub ApplyFormatting(ByRef node As HtmlNode)
            Dim changeList() As String = {"b""strong""i""em"}
            Dim changeToStyleList() As String = {"font-weight:bold;""font-weight:bold;""font-style:italic;", _
                                                 "font-style:italic;"}
            For index As Integer = 0 To changeList.Count() - 1
                Dim changeNodes As HtmlNodeCollection = node.SelectNodes("//" + changeList(index))
                If (changeNodes IsNot Nothing AndAlso changeNodes.Count() > 0) Then
                    For Each changeNode As HtmlNode In changeNodes
                        changeNode.SetAttributeValue("style", changeToStyleList(index))
                    Next
                End If
            Next
        End Sub
        Private Shared Function StripStyles(ByVal html As StringAs String
            Return Regex.Replace(html, "<style.*?</style>""", RegexOptions.Singleline Or RegexOptions.IgnoreCase)
        End Function
    End Class 

ExportRE.Content = PdfUtil.PdfFilter(ContentLiteral.Text, True)
ExportRE.ExportToPdf() 
0
Rumen
Telerik team
answered on 14 May 2010, 10:00 AM
Hi Eirik,

Thank you for sharing this pack with the community!

I just want to add that we provided a new PdfExportFilter content filter that handles the tables when they are exported to PDF so the developer should not write any additional code. You can test the Export To PDF demo for more information.

We are also continuously enhancing the filter so that more scenarios are supported when exporting to PDF.

Best regards,
Rumen
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Eirik H
Top achievements
Rank 2
answered on 14 May 2010, 11:43 AM
@Rumen

Thanks, I already know about and use the PdfExportFilter, but I've noticed sometimes the number of cols isn't equal to the number of td's, e.g. something like:

<colgroup> 
 <col /><col /> 
</colgroup> 
<tr> 
 <td></td
 <td></td
 <td></td
</tr> 

I'm guessing around 5 % of the tables users have created in our editor has this problem, but I'm not quite sure how they've managed to do so.

If the PdfExportFilter is based on some javascript posted by yourself some time ago ( http://www.telerik.com/community/forums/aspnet-ajax/editor/export-to-pdf-doc.aspx#928256 ) you skip creating colgroup and cols if there already is a colgroup:

//If there are colgroups - skip table  
var groups = table.getElementsByTagName("colgroup");  
if (groups.length > 0) continue;//!  

So I'm guessing that if somehow a table goes from having e.g. 3 td's for each row to 4, the colgroup isn't updated to reflect this. That's why I'm just throwing away the colgroup and creating a new one.

Tags
Editor
Asked by
Fred
Top achievements
Rank 1
Answers by
Rumen
Telerik team
Eirik H
Top achievements
Rank 2
Share this question
or