Ticket T336404
Visible to All Users
Duplicate

We have closed this ticket because another page addresses its subject:

Provide the capability to specify paper settings for the printed document

How to convert HTML to Landscape PDF

created 9 years ago

I have HTML rendered from a list view:
Dim objSW As New System.IO.StringWriter()
Dim objHTW As New System.Web.UI.HtmlTextWriter(objSW)
lvOutput.RenderControl(objHTW)
Session("output") = objSW.ToString

I need this string to be exported to PDF in landscape with minimal margins. I tried using the ASPxHTMLEditor and setting the html property equal to the string, then using htmlSI.Export(HtmlEditorExportFormat.Pdf, pdfName). The PDF gets created and downloaded, but there is no way to change the settings.

I saw a post about importing the html to a RichEdit, then exporting to pdf but couldn't find any code examples of that full process. Is this still the best way to accomplish my task?

Can you provide a code example that will take my html string and produce a PDF with margins and landscape orientation?

Answers approved by DevExpress Support

created 9 years ago (modified 9 years ago)

So here is the final complete solution to my question. This will render the HTML from a listView control, and download a PDF in landscape with minimal margins:

Dim objSW As New System.IO.StringWriter()
        Dim objHTW As New System.Web.UI.HtmlTextWriter(objSW)
        lvSignIn.RenderControl(objHTW)
        htmlSI.Html = objSW.ToString 'This assigns my rendered HTML to an APSxHtmlEditor control
        Using ms_output = New MemoryStream
            Dim Server As RichEditDocumentServer = New RichEditDocumentServer()
            Using srv As New RichEditDocumentServer()
                PrepareDocumentModel(htmlSI, srv.Model) 'assign the contents of the ASPxHtmlEditor to the RichEditDocumentServer
                Using ps As New DevExpress.XtraPrinting.PrintingSystemBase()
                    Using pcl As New DevExpress.XtraPrintingLinks.PrintableComponentLinkBase(ps)
                        pcl.Component = srv
                        'Here you can configure exporting document options, e.g. margins
                        Dim section As DevExpress.XtraRichEdit.API.Native.Section = srv.Document.Sections(0)
                        section.Page.Landscape = True
                        section.Margins.Top = 25
                        section.Margins.Left = 25
                        section.Margins.Bottom = 25
                        section.Margins.Right = 25
                        pcl.CreateDocument()
                        srv.ExportToPdf(ms_output) 'Export the contents of the RichEditDocumentServer as a PDF to a memoryStream
                        ms_output.Position = 0
                        'Send the memoryStream to the Page Response (download it)
                        ms_output.CopyTo(Page.Response.OutputStream)
                        Page.Response.ContentType = "application/pdf"
                        Page.Response.HeaderEncoding = System.Text.Encoding.UTF8
                        Page.Response.AddHeader("Content-Disposition", "attachment; filename=SIS.pdf")
                        Page.Response.End()
                        ms_output.Close()
                    End Using
                End Using
            End Using
        End Using

    Comments (1)
    DevExpress Support Team 9 years ago

      Hello,
      Thank you for sharing your solution. We greatly appreciate it.

      created 9 years ago

      Hi Jennifer,

      Use the approach provided in the S37898: Provide the capability to specify paper settings for the printed document thread to accomplish this task. With this approach, you will be able to specify the required PDF paper settings by using the PrintableComponentLinkBase class members. For example, use the Landscape property to enable the landscape mode.
      Let us know if you have any further questions regarding this.

        Show previous comments (2)

          No.
          Did you read my question?
          I'm not starting from a file. I'm starting from a string. It's html created from rendering a list view.
          So your code example, "server.LoadDocument(filePath)" is irrelevant.
          Please, pretty please, provide a complete working code example starting from a string of html and resulting in a landscape PDF where I can specify margins.

            Ok, so I spent another hour messing with the code based on what you sent and i may have gotten a step further but I still don't have a complete solution. I don't want to use the file system. This exports a blank PDF that is in landscape. My previous code which used the same stream passed into the sub routine was not blank but it was also not landscape.  PLEASE PLEASE PLEASE - I'm struggling here - provide a complete code example from:
            htmlSI.Html = objSW.ToString
            Dim myStream As New MemoryStream
            ExportToStream(htmlSI, myStream)
            to the actual PDF downloading.
            Using server As New RichEditDocumentServer()
                      server.LoadDocument(stream, DevExpress.XtraRichEdit.DocumentFormat.Html)
                      Dim section As DevExpress.XtraRichEdit.API.Native.Section = server.Document.Sections(0)
                      section.Page.Landscape = True
                      section.Margins.Top = 25
                      section.Margins.Left = 25
                      section.Margins.Right = 25
                      section.Margins.Bottom = 25
                      Dim memoryStream As New MemoryStream()
                     server.ExportToPdf(memoryStream)
                     memoryStream.Position = 0
                      memoryStream.WriteTo(Page.Response.OutputStream)
                      Page.Response.ContentType = "application/pdf"
                      Page.Response.HeaderEncoding = System.Text.Encoding.UTF8
                      Page.Response.AddHeader("Content-Disposition", "attachment; filename=SIS.pdf")
                      Page.Response.End()
                  End Using

            DevExpress Support Team 9 years ago

              Hello,
              I'm using a file in my example to demonstrate that the export to PDF works properly in case if the document orientation is 'Landscape'.
              PDF can be exported as a blank document because you need to set a stream position to '0' before loading the stream to RichEdit Document Server.
              I have attached a sample project and video to illustrate that RichEdit Document Server exports the document with landscape orientation to PDF correctly.
              If this solution does not work for you, please modify it to demonstrate the issue.

              Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

              Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.