0

Here in my Asp.net web application I have to generat pdf, earlier I used to achive this task using itextsharp (server side / paid services) , now i found jspdf can be done on client side plus point it is open source.

Heres sample code which generate pdf

  var doc = new jsPDF();
  doc.text(20, 20, 'Hello world!');
  doc.text(20, 30, 'This is client-side Javascript, pumping out a PDF.');
  doc.addPage();
  doc.text(20, 20, 'Do you like that?');
  doc.save('Test.pdf');

For real work I need to get data by applying some logic (server side coding) , so i want to know whether i can access doc on serverside i.e ( c# ) or any another way to do ?

xanatos
  • 109,618
  • 12
  • 197
  • 280
Satinder singh
  • 10,100
  • 16
  • 60
  • 102
  • @Juhana: thank you, but none of the answer was right marked, and it also tagged as `asp.net mvc`, where mine is simple asp.net c# – Satinder singh Aug 05 '13 at 10:06
  • The principle is exactly the same regardless of what the server-side technology is. That none of the answers are accepted doesn't mean that they wouldn't be correct. – JJJ Aug 05 '13 at 10:07

1 Answers1

2

You can't access the doc object (the jsPDF object). What you can do is that the client asks the server the data it needs through a webservice or some rest api, OR if what you need is pdf signing, the client could send the pdf to the server in some way (through a webservice or some rest api) and then the server could send back the pdf... But this would make it useless to use a client-side pdf generation.

There are some other options: if you make the button that generates the PDF make a roundtrip to the server (and then after the roundtrip it launch the generation of the PDF), then some additional data can be inserted in the page by the server, so

<asp:Button ID="btnPdf" runat="server" Text="Generate PDF" OnClick="btnPdf_Click" />

and in btnPdf_Click:

ClientScriptManager.RegisterStartupScript(this.GetType(), "PdfKey", "GeneratePdf();", true);

and in the Javascript:

function GeneratePdf()
{
    // If it's a string, it's better that you escape the content of <%= %>
    // for example with HttpUtility.JavaScriptStringEncode in ASP.NET 4.0
    var serverData = <%= SomeNetVariableFilledByBtnPdf_Click %>;
    // generate the pdf
}
xanatos
  • 109,618
  • 12
  • 197
  • 280