Open PDF from html document

17 posts, 0 answers
  1. Runjith
    Runjith avatar
    26 posts
    Member since:
    Mar 2014

    Posted 14 Aug 2014 Link to this post

    Hi,

    I have html document(xyz.html),Under click event i want to open PDF file from html Document using Telerik.

    Please let me know how can I proceed.

    Regards,
    Ranjith
  2. Petya
    Admin
    Petya avatar
    975 posts

    Posted 19 Aug 2014 Link to this post

    Hello Ranjith,

    RadRichTextBox exposes several format providers that allow you to convert a document from one type to another. In your case you can import the HTML with HtmlFormatProvider and export it to PDF with the PdfFormatProvider class. More information is available in the Import/Export help article.

    Once you've generated the desired PDF you can use the RadPdfViewer component which is part of the Telerik UI for WPF suite to show it. I suggest you check the documentation of the control as well as its SDK examples: RadPdfViewer OverviewShowing a FileSDK Samples Browser.

    I hope this is helpful.

    Regards,
    Petya
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. UI for WPF is Visual Studio 2017 Ready
  4. Runjith
    Runjith avatar
    26 posts
    Member since:
    Mar 2014

    Posted 22 Aug 2014 in reply to Petya Link to this post

    Hi,

    Please find the below code,while opening pdf from html file.

    ​ string html = File.ReadAllText(ViewDetails.html);
    string pdfPath = strDestinationPath + strSessionID + _Params.CustomizeID +"Configuration Details.pdf";
    HtmlFormatProvider provider = new HtmlFormatProvider();
    RadDocument document = provider.Import(html);
    PdfFormatProvider pdf = new PdfFormatProvider();
    var bytes= pdf.Export(document);
    if (File.Exists(pdfPath))
    {
    File.SetAttributes(pdfPath, FileAttributes.Normal);
    }

    var fs = new FileStream(pdfPath, FileMode.Create);
    fs.Write(bytes, 0, bytes.Length);
    fs.Close();
    Process.Start(pdfPath);

    It is displaying empty PDF file.But "ViewDetails.html" having data.If i open in browser it will display the datails.

    How can i display Html data into PDF.

    Regards,
    Ranjith

  5. Petya
    Admin
    Petya avatar
    975 posts

    Posted 26 Aug 2014 Link to this post

    Hello Ranjith,

    I am not sure what might be causing this result on your end. Could you confirm this is the behavior with all HTML documents or does the issue depend on the HTML content? Please also try the behavior of the attached application, the source HTML it uses is located in the \ExportPdfDemo\ExportPdfDemo\bin\Debug\SampleData folder.

    I'm looking forward to your reply.

    Regards,
    Petya
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  6. Runjith
    Runjith avatar
    26 posts
    Member since:
    Mar 2014

    Posted 28 Aug 2014 in reply to Petya Link to this post

    Hi,

    When i add this code

     string html = File.ReadAllText(@"C:\Users\ranjith.p01\Downloads\ExportPdfDemo\bin\Debug\SampleData\test.html");
                string pdfPath = "SampleData\\Configuration Details.pdf";

                HtmlFormatProvider provider = new HtmlFormatProvider();
                RadDocument document = provider.Import(html);
                PdfFormatProvider pdf = new PdfFormatProvider();

                var bytes = pdf.Export(document);
                if (File.Exists(pdfPath))
                {
                    File.SetAttributes(pdfPath, FileAttributes.Normal);
                }

                var fs = new FileStream(pdfPath, FileMode.Create);
                fs.Write(bytes, 0, bytes.Length);
                fs.Close();
                Process.Start(pdfPath);

    I am getting Error:The type initializer for 'Telerik.Windows.Documents.Model.RadDocument' threw an exception.

    near this line: RadDocument document = provider.Import(html);

    Please give me reply ASAP.



    Regards,
    Ranjith
  7. Runjith
    Runjith avatar
    26 posts
    Member since:
    Mar 2014

    Posted 28 Aug 2014 in reply to Runjith Link to this post

    Hi,

    It is working fine with html which you have attached.But if i give my own HTML file it is not working.

    Please tell me what is the problem here.

    Regards,
    Ranjith
  8. Runjith
    Runjith avatar
    26 posts
    Member since:
    Mar 2014

    Posted 01 Sep 2014 in reply to Runjith Link to this post

    Working fine without errors, but It Opens empty PDF file.
    Not loading my Html data.

    Regards,
    Ranjith
  9. Petya
    Admin
    Petya avatar
    975 posts

    Posted 01 Sep 2014 Link to this post

    Hello Ranjith,

    Could you provide us with the source document that you are having troubles exporting? I'm afraid that I am unable to comment on what might be causing this without investigating the HTML first. 

    Note that the public forums do not allow attachments. You can open a support ticket and attach the file there.

    Looking forward to hearing from you.

    Regards,
    Petya
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  10. Runjith
    Runjith avatar
    26 posts
    Member since:
    Mar 2014

    Posted 02 Sep 2014 in reply to Petya Link to this post

    Hi,

    I have raised the support ticket.

    Please find the ticket id.
    855410 (view all your support tickets)
  11. Petya
    Admin
    Petya avatar
    975 posts

    Posted 04 Sep 2014 Link to this post

    Hello,

    For anyone else that might encounter this topic, the issue was caused due to specific content in the HTML that prevents the document from being imported in the control. The public item for this can be found at Documents with STYLE tag are not imported properly.

    Regards,
    Petya
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  12. Runjith
    Runjith avatar
    26 posts
    Member since:
    Mar 2014

    Posted 16 Sep 2014 in reply to Petya Link to this post

    Hi,
    Please find the attached PDF screens.

    How to remove the space between line to line and How to reduce font size in PDF document.

    I am using below code for converting HTML to PDF.

                string html = File.ReadAllText(path);
               // string html = File.ReadAllText(@"C:\Test1.html");
                string pdfPath = "C:\\Configuration Details.pdf";
                HtmlImportSettings uri = new HtmlImportSettings();
                uri.LoadImageFromUrl += uri_LoadImageFromUrl;
                
                HtmlFormatProvider provider = new HtmlFormatProvider();
                provider.ImportSettings = uri;
                RadDocument document = provider.Import(html);
                document.LayoutMode = DocumentLayoutMode.Paged;
                PdfFormatProvider pdf = new PdfFormatProvider();

                var bytes = pdf.Export(document);
                if (File.Exists(pdfPath))
                {
                    File.SetAttributes(pdfPath, FileAttributes.Normal);
                }

                var fs = new FileStream(pdfPath, FileMode.Create);
                fs.Write(bytes, 0, bytes.Length);
                fs.Close();
                Process.Start(path);
                Process.Start(pdfPath);

    Regards,
    Ranjith
  13. Runjith
    Runjith avatar
    26 posts
    Member since:
    Mar 2014

    Posted 16 Sep 2014 in reply to Runjith Link to this post

    Data coming from Different XSL's.It 's looking like between one xsl data to another xsl data having space,how to remove that?

    Regards,
    Ranjith
  14. Runjith
    Runjith avatar
    26 posts
    Member since:
    Mar 2014

    Posted 17 Sep 2014 in reply to Runjith Link to this post

    Hi,

    When i open PDF document,i can see the zoom percentage of the document is 139(%).
    I want to set(Default) 100%.
    Find the attached document.
    Please help me how can i fix this.

    Regards,
    Ranjith
  15. Petya
    Admin
    Petya avatar
    975 posts

    Posted 19 Sep 2014 Link to this post

    Hello Runjith,

    Without the actual HTML, we would be guessing what is causing the empty space in your document. You could try showing the document in RadRichTextBox prior the export and enabling the formatting symbols to see whether they are additional paragraphs in that place. Alternatively, send us your HTML document so that we can look into the cause.

    Regarding the font size, you can reduce it by selecting the desired content and calling the ChangeFontSize() method of RadRichTextBox or RadDocumentEditor. This article provides more details on document selection.

    As to the scale factor when showing a document, such settings are not supported by the PdfFormatProvider so this is not information that is exported in the PDF file. The scale factor with which a document is loaded depends on the software that shows the PDF.

    Regards,
    Petya
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  16. Runjith
    Runjith avatar
    26 posts
    Member since:
    Mar 2014

    Posted 22 Sep 2014 in reply to Petya Link to this post

    Hi,

    If i give border="2" for tables in html doc,used to display border for tables in PDF doc.

    after applying below code i am not getting any borders for my table.please find the bellow code and help me how can i get borders.

     HtmlDataProvider htmlDataProvider = new HtmlDataProvider();
      htmlDataProvider.SetupDocument += htmlDataProvider_SetupDocument; 

     void htmlDataProvider_SetupDocument(object sender, Telerik.Windows.Documents.FormatProviders.SetupDocumentEventArgs e)
            {
                foreach (Telerik.Windows.Documents.Model.Span span in e.Document.EnumerateChildrenOfType<Telerik.Windows.Documents.Model.Span>())
                {
                    span.ClearValue(Telerik.Windows.Documents.Model.Span.FontSizeProperty);
                    span.ClearValue(Telerik.Windows.Documents.Model.Span.FontFamilyProperty);
                    span.ClearValue(Telerik.Windows.Documents.Model.Span.FontStyleProperty);
                    span.ClearValue(Telerik.Windows.Documents.Model.Span.FontWeightProperty);
                }
            }

    Regards,
    Ranjith
  17. Runjith
    Runjith avatar
    26 posts
    Member since:
    Mar 2014

    Posted 22 Sep 2014 in reply to Runjith Link to this post

    I Commented and tried,still i am not getting? What will be the problem please help me...................


    Regards,
    Ranjith
  18. Runjith
    Runjith avatar
    26 posts
    Member since:
    Mar 2014

    Posted 22 Sep 2014 in reply to Runjith Link to this post

    Done,Its working fine.I changed the name of the PDF.
Back to Top
UI for WPF is Visual Studio 2017 Ready