How To Embed Javascript in PDF

10 posts, 0 answers
  1. briankb
    briankb avatar
    88 posts
    Member since:
    Feb 2004

    Posted 02 May 2008 Link to this post

    I just started playing with Telerik Reporting. I'm hoping to migrate a simple single page report I currently do in .net using PDFGenerator.

    With PDFGenerator you can embed javascript. In my case I add the following line:
    MyDocument.JavaScripts.Add(New DocumentJavaScript("print", "this.print({bUI: false, bSilent: true, bShrinkToFit: true});"))

    Which results in the PDF file going straight to the printer.

    I created the following code in a new .aspx file using sample code I found in the docs.

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
            Dim report As New Telerik.Reporting.Report() 
            Dim detail As New Telerik.Reporting.DetailSection() 
            Dim textBox2 As New Telerik.Reporting.TextBox() 
            textBox2.Location = New Telerik.Reporting.Drawing.PointU(New Telerik.Reporting.Drawing.Unit(0, Telerik.Reporting.Drawing.UnitType.Inch), New Telerik.Reporting.Drawing.Unit(0, Telerik.Reporting.Drawing.UnitType.Inch)) 
            textBox2.Value = "Hello World!" 
            textBox2.Location = New Telerik.Reporting.Drawing.PointU(New Telerik.Reporting.Drawing.Unit(1.25, Telerik.Reporting.Drawing.UnitType.Inch), New Telerik.Reporting.Drawing.Unit(0, Telerik.Reporting.Drawing.UnitType.Inch)) 
            textBox2.Size = New Telerik.Reporting.Drawing.SizeU(New Telerik.Reporting.Drawing.Unit(4.75, Telerik.Reporting.Drawing.UnitType.Inch), New Telerik.Reporting.Drawing.Unit(0.25, Telerik.Reporting.Drawing.UnitType.Inch)) 
            detail.Items.AddRange(New Telerik.Reporting.ReportItemBase() {textBox2}) 
            detail.Height = New Telerik.Reporting.Drawing.Unit(0.3, Telerik.Reporting.Drawing.UnitType.Inch) 
            report.Items.Add(DirectCast(detail, Telerik.Reporting.ReportItemBase)) 
            ExportToPDF("Test", report) 
        End Sub 
     
        Sub ExportToPDF(ByVal reportName As String, ByVal reportToExport As Telerik.Reporting.Report) 
            Dim mimeType As StringString = String.Empty 
            Dim ext As StringString = String.Empty 
            Dim encoding As EncodingEncoding = Encoding.[Default] 
            Dim reportBytes As Byte() = ReportProcessor.Render("PDF", reportToExport, Nothing, mimeType, ext, encoding) 
            Dim fileName As String = reportName + ".pdf" 
            Response.Clear() 
            Response.ContentType = mimeType 
            Response.Cache.SetCacheability(HttpCacheability.[Private]) 
            Response.Expires = -1 
            Response.Buffer = False 
            Response.AddHeader("Content-Disposition", String.Format("{0};FileName=""{1}""", "attachment", fileName)) 
            Response.OutputStream.Write(reportBytes, 0, reportBytes.Length) 
            Response.[End]() 
        End Sub 

    This code generates a PDF directly just like my PDFGenerator code.

    How do I embed javascript directly in the PDF generated by the Telerik Reporting control?



  2. Rossen Hristov
    Admin
    Rossen Hristov avatar
    2478 posts

    Posted 02 May 2008 Link to this post

    Hello Briankb,

    Thank you for contacting Telerik Support.

    Currently there is no way to inject JavaScript in the generated PDF file. We will log that as a feature request and if there is enough interest we will implement it for a future release. Thanks for the great idea.

    All the best,
    Ross
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. DevCraft banner
  4. briankb
    briankb avatar
    88 posts
    Member since:
    Feb 2004

    Posted 02 May 2008 Link to this post

    Thanks for the quick reply.

    I hope you will add it soon. Or if you don't want to open it up to javascript maybe make a property you can set true/false in the PDF generation where the javascript I mentioned earlier is added which pushes the PDF directly to the default printer or at least the printer dialog box.
  5. Chavdar
    Admin
    Chavdar avatar
    898 posts

    Posted 02 May 2008 Link to this post

    Hi Briankb,

    You can start the immediate print after sending the pdf document by adding the following device information:

        Dim deviceInfo As New Hashtable()
        deviceInfo.Add("ImmediatePrint", True)

        ReportProcessor.Render("PDF", reportToExport, deviceInfo, mimeType, ext, encoding) 

    However, it will first show the Print dialog before printing. Hope this helps.

    Regards,
    Chavdar
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  6. Jun
    Jun avatar
    47 posts
    Member since:
    Sep 2006

    Posted 10 May 2008 Link to this post

    How can you hide or disable the print button in the PDF reader? Is this can be done by injecting javascript? Or one of you suggestions like device setting?

    Bembol
  7. Chavdar
    Admin
    Chavdar avatar
    898 posts

    Posted 12 May 2008 Link to this post

    Hi bembol,

    At this time you cannot hide or disable the Print button in the reader. We are currently researching whether this is possible or not. If possible we will include the requested functionality for the service pack release in a couple of weeks.

    All the best,
    Chavdar
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  8. Rakesh
    Rakesh avatar
    1 posts
    Member since:
    Jul 2008

    Posted 15 Jul 2008 Link to this post

    Hi,
    I don't know how Chavdar has written that you can't disable the printing options. You can disable both printing as wel as copying options too, so that the document can neither be copied or printed. This can be achieved while creating the document itself. I use openOffice for creating pdf documents. Here open any word document and then
    File>Export as PDF> give file name abc.pdf >save
    pdfOptions page pops up : Here  under the security options check the  Restrict the Permissions then click set permission Password and after setting the password choose the permissions.

  9. Steve
    Admin
    Steve avatar
    10941 posts

    Posted 15 Jul 2008 Link to this post

    Hello Rakesh,

    We believe that you are talking about something else - generating the pdfs yourself with other 3rd party software. While the conversation here is about the auto generated pdf files by our Reporting tool. Our developers have already implemented a way to inject javascript in the generated pdf - through the javascript attribute. More info is available in this help topic.

    Kind regards,
    Steve
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  10. sundarabalaji
    sundarabalaji avatar
    1 posts
    Member since:
    Apr 2015

    Posted 01 Apr 2015 in reply to Chavdar Link to this post

    hi bembol,

    Can u send example code for Render Concept
  11. KS
    KS avatar
    165 posts
    Member since:
    Oct 2012
Back to Top
DevCraft banner