• We just launched and are currently in beta. Join us as we build and grow the community.

Generating PDF File using JavaScript and JSPDF Library Tutorial

Anxious

Clickbank Wizard
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
84
Likes
153
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial, you will learn how to Generate or Create a PDF File for your web applications on the client side using JavaScript and JSPDF Library. The tutorial aims to provide IT/CS students and new web developers with a reference for learning how to generate PDF files using JavaScript. Here, simple snippets that demonstrate the goal of this tutorial are provided. A sample and working source code zip file is also provided and is free to download.

Why implement a PDF Generation in web applications?

Developers implement a PDF Generation Feature in web applications to achieve the requirement of the system that they are building. It is one of the common features that end-users asked or look for in a web application, especially when exporting some information or reports from the system.

How to generate PDF Files using JavaScript?

There are lots of JS Libraries that are available online that were built for Generating PDF Files in web applications. One of these libraries is the JSPDF Library. JSPDF Library is a free JS library for generating PDFs. See the Installation process of the JSPDF Library on Github.

Demo Snippets

Here are some sample snippets for using the JSPDF Library.

Simple Generating PDF

The following snippet demonstrates how to generate a PDF File. The script explains how to add content on a PDF per line or paragraph. The snippet loads the Bootstrap Framework using CDNs which requires an Internet connection to display the User Interface properly.

Interface

  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    >
  5. <meta

    http-equiv

    =

    "X-UA-Compatible"

    content

    =

    "IE=edge"

    >
  6. <meta

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1.0"

    >
  7. <title

    >

    jsPDF Library Demo</

    title

    >
  8. <link

    rel

    =

    "stylesheet"

    href

    =

    "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css"

    integrity=

    "sha512-xh6O/CkQoPOWDdYTDqeRdPCVd1SpvCA9XXcUnZS2FmJNp1coAFzvtCN9BmamE+4aHK8yyUHUSCcJHgXloTyT2A=="

    crossorigin=

    "anonymous"

    referrerpolicy=

    "no-referrer"

    /

    >
  9. <link

    rel

    =

    "stylesheet"

    href

    =

    "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"

    integrity=

    "sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi"

    crossorigin=

    "anonymous"

    >
  10. <link

    rel

    =

    "stylesheet"

    href

    =

    "style.css"

    >

  11. <script

    src

    =

    "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/js/all.min.js"

    integrity=

    "sha512-naukR7I+Nk6gp7p5TMA4ycgfxaZBJ7MO5iC3Fp6ySQyKFHOGfpkSZkYVWV5R7u7cfAicxanwYQ5D1e17EfJcMA=="

    crossorigin=

    "anonymous"

    referrerpolicy=

    "no-referrer"

    ></

    script

    >
  12. <script

    src

    =

    "./jquer-3.6.1.min.js"

    ></

    script

    >
  13. <script

    src

    =

    "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"

    integrity=

    "sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3"

    crossorigin=

    "anonymous"

    ></

    script

    >
  14. <!-- HTML2 Canvas Library -->
  15. <script

    src

    =

    "./html2canvas.min.js"

    ></

    script

    >
  16. <!-- JSPDF Library -->
  17. <script

    src

    =

    "./jspdf.debug.js"

    ></

    script

    >
  18. </

    head

    >
  19. <body

    >
  20. <nav class

    =

    "navbar navbar-expand-lg navbar-dark bg-primary bg-gradient"

    >
  21. <div

    class

    =

    "container"

    >
  22. <a

    class

    =

    "navbar-brand"

    href

    =

    "./"

    >

    jsPDF Library - DEMO</

    a

    >
  23. <div

    >
  24. <a

    href

    =

    "https://sourcecodester.com"

    class

    =

    "text-light fw-bolder h6 text-decoration-none"

    target

    =

    "_blank"

    >

    SourceCodester</

    a

    >
  25. </

    div

    >
  26. </

    div

    >
  27. </

    nav>
  28. <div

    class

    =

    "container-fluid px-5 my-3"

    >
  29. <div

    class

    =

    "col-lg-6 col-md-8 col-sm-12 mx-auto"

    >
  30. <div

    class

    =

    "card rounded-0"

    >
  31. <div

    class

    =

    "card-header"

    >
  32. <div

    class

    =

    "d-flex w-100 align-items-end"

    >
  33. <div

    class

    =

    "col-auto flex-shrink-1 flex-grow-1"

    >
  34. <div

    class

    =

    "card-title text-muted"

    ><b

    >

    Simple PDF Content</

    b

    ></

    div

    >
  35. </

    div

    >
  36. <div

    class

    =

    "col-auto flex-shrink-1"

    >
  37. <button

    class

    =

    "btn btn-primary rounded-0 btn-sm"

    type

    =

    "button"

    id

    =

    "simple_pdf"

    >

    Export to PDF</

    button

    >
  38. </

    div

    >
  39. </

    div

    >

  40. </

    div

    >
  41. <!-- Simple Exporting PDf Card -->
  42. <div

    class

    =

    "card-body rounded-0"

    >
  43. <div

    class

    =

    "container-fluid"

    >
  44. <div

    id

    =

    "simple_pdf_container"

    class

    =

    "page-container"

    >
  45. <p

    >

    jsPDF Library DEMO</

    p

    >
  46. <p

    >

    I love SourceCodester</

    p

    >
  47. <p

    >

    Lorem ipsum dolor sit amet consectetur, adipisicing elit. Sed alias nam nesciunt soluta unde nemo provident vero sit architecto veniam expedita ab, non illum nulla pariatur error magni! Ducimus, facere.</

    p

    >
  48. <p

    ></

    p

    >
  49. <p

    >

    I love SourceCodester</

    p

    >
  50. </

    div

    >
  51. </

    div

    >
  52. </

    div

    >
  53. <!-- End of Simple Exporting PDf Card -->
  54. </

    div

    >
  55. </

    div

    >
  56. </

    div

    >
  57. <script

    src

    =

    "./script.js"

    ></

    script

    >
  58. </

    body

    >
  59. </

    html

    >

Output

JavaScript

  1. var

    simple_pdf_btn =

    document.getElementById

    (

    'simple_pdf'

    )
  2. window.simple_pdf

    =

    function

    (

    )

    {
  3. var

    container =

    document.getElementById

    (

    'simple_pdf_container'

    )
  4. let pdf =

    new

    jsPDF(

    )

    ;

  5. var

    p =

    container.querySelectorAll

    (

    'p'

    )
  6. var

    line =

    10

    ;
  7. p.forEach

    (

    el =>

    {
  8. pdf.text

    (

    pdf.splitTextToSize

    (

    el.innerText

    ,

    180

    )

    ,

    10

    ,

    line)

    ;
  9. line +=

    10
  10. }

    )
  11. pdf.save

    (

    'simple_pdf.pdf'

    )
  12. }
  13. window.onload

    =

    function

    (

    )

    {

  14. simple_pdf_btn.addEventListener

    (

    'click'

    ,

    function

    (

    e)

    {
  15. e.preventDefault

    (

    )
  16. simple_pdf(

    )
  17. }

    )

  18. }

Output

Converting HTML Elements to PDF

The following snippet demonstrates how to generate a PDF File that from using the HTML Node as the content.

Interface

  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    >
  5. <meta

    http-equiv

    =

    "X-UA-Compatible"

    content

    =

    "IE=edge"

    >
  6. <meta

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1.0"

    >
  7. <title

    >

    jsPDF Library Demo</

    title

    >
  8. <link

    rel

    =

    "stylesheet"

    href

    =

    "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css"

    integrity=

    "sha512-xh6O/CkQoPOWDdYTDqeRdPCVd1SpvCA9XXcUnZS2FmJNp1coAFzvtCN9BmamE+4aHK8yyUHUSCcJHgXloTyT2A=="

    crossorigin=

    "anonymous"

    referrerpolicy=

    "no-referrer"

    /

    >
  9. <link

    rel

    =

    "stylesheet"

    href

    =

    "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"

    integrity=

    "sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi"

    crossorigin=

    "anonymous"

    >
  10. <link

    rel

    =

    "stylesheet"

    href

    =

    "style.css"

    >

  11. <script

    src

    =

    "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/js/all.min.js"

    integrity=

    "sha512-naukR7I+Nk6gp7p5TMA4ycgfxaZBJ7MO5iC3Fp6ySQyKFHOGfpkSZkYVWV5R7u7cfAicxanwYQ5D1e17EfJcMA=="

    crossorigin=

    "anonymous"

    referrerpolicy=

    "no-referrer"

    ></

    script

    >
  12. <script

    src

    =

    "./jquer-3.6.1.min.js"

    ></

    script

    >
  13. <script

    src

    =

    "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"

    integrity=

    "sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3"

    crossorigin=

    "anonymous"

    ></

    script

    >
  14. <!-- HTML2 Canvas Library -->
  15. <script

    src

    =

    "./html2canvas.min.js"

    ></

    script

    >
  16. <!-- JSPDF Library -->
  17. <script

    src

    =

    "./jspdf.debug.js"

    ></

    script

    >
  18. </

    head

    >
  19. <body

    >
  20. <nav class

    =

    "navbar navbar-expand-lg navbar-dark bg-primary bg-gradient"

    >
  21. <div

    class

    =

    "container"

    >
  22. <a

    class

    =

    "navbar-brand"

    href

    =

    "./"

    >

    jsPDF Library - DEMO</

    a

    >
  23. <div

    >
  24. <a

    href

    =

    "https://sourcecodester.com"

    class

    =

    "text-light fw-bolder h6 text-decoration-none"

    target

    =

    "_blank"

    >

    SourceCodester</

    a

    >
  25. </

    div

    >
  26. </

    div

    >
  27. </

    nav>
  28. <div

    class

    =

    "container-fluid px-5 my-3"

    >

  29. <div

    class

    =

    "col-lg-6 col-md-8 col-sm-12 mx-auto"

    >
  30. <!-- Converting and Exporting an HTML Element as PDF -->
  31. <div

    class

    =

    "card rounded-0"

    >
  32. <div

    class

    =

    "card-header"

    >
  33. <div

    class

    =

    "d-flex w-100 align-items-end"

    >
  34. <div

    class

    =

    "col-auto flex-shrink-1 flex-grow-1"

    >
  35. <div

    class

    =

    "card-title text-muted"

    ><b

    >

    Exporting HTML Element to PDF</

    b

    ></

    div

    >
  36. </

    div

    >
  37. <div

    class

    =

    "col-auto flex-shrink-1"

    >
  38. <button

    class

    =

    "btn btn-primary rounded-0 btn-sm"

    type

    =

    "button"

    id

    =

    "html_pdf"

    >

    Export Element to PDF</

    button

    >
  39. </

    div

    >
  40. </

    div

    >
  41. </

    div

    >
  42. <div

    class

    =

    "card-body rounded-0"

    >
  43. <div

    class

    =

    "container-fluid"

    >
  44. <div

    id

    =

    "html_pdf_container"

    class

    =

    "page-container"

    >
  45. <h2

    >

    Exported HTML</

    h2

    >
  46. <p

    >
  47. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque est odio, lobortis eget pharetra et, commodo non felis. Pellentesque dui dolor, lobortis et blandit vel, commodo nec tellus. Nunc ac metus iaculis, scelerisque quam et, luctus ipsum. Proin euismod nec mi sed venenatis. Suspendisse id nibh ipsum. Sed vestibulum vulputate purus, ac ultricies nulla finibus at. Maecenas vel lacus erat. Nam odio magna, porttitor luctus efficitur ac, auctor sit amet lorem. Etiam ultrices tempus molestie. Sed sagittis nulla nec nibh auctor finibus.
  48. </

    p

    >

  49. <h2

    >

    Table</

    h2

    >
  50. <table

    class

    =

    "table table-bordered"

    >
  51. <tr

    >
  52. <th

    >

    Header 1</

    th

    >
  53. <th

    >

    Header 2</

    th

    >
  54. <th

    >

    Header 3</

    th

    >
  55. </

    tr

    >
  56. <tr

    >
  57. <td

    >

    Column 1</

    td

    >
  58. <td

    >

    Column 2</

    td

    >
  59. <td

    >

    Column 3</

    td

    >
  60. </

    tr

    >
  61. <tr

    >
  62. <td

    >

    Column 2.1</

    td

    >
  63. <td

    >

    Column 2.2</

    td

    >
  64. <td

    >

    Column 2.3</

    td

    >
  65. </

    tr

    >
  66. </

    table

    >

  67. </

    div

    >
  68. </

    div

    >
  69. </

    div

    >
  70. </

    div

    >
  71. <!-- End of Converting and Exporting an HTML Element as PDF -->
  72. </

    div

    >
  73. </

    div

    >
  74. <script

    src

    =

    "./script.js"

    ></

    script

    >
  75. </

    body

    >
  76. </

    html

    >

Output

JavaScript

  1. var

    html_pdf_btn =

    document.getElementById

    (

    'html_pdf'

    )

  2. window.html_pdf

    =

    function

    (

    )

    {
  3. var

    container =

    document.getElementById

    (

    'html_pdf_container'

    )

    .cloneNode

    (

    true

    )

  4. let pdf =

    new

    jsPDF(

    'p'

    ,

    'pt'

    ,

    'letter'

    )

    ;
  5. container.querySelectorAll

    (

    'table'

    )

    .forEach

    (

    el=>

    {
  6. el.removeAttribute

    (

    'class'

    )
  7. el.style

    .borderCollapse

    =

    'collapse'
  8. el.style

    .width

    =

    '100%'
  9. el.querySelectorAll

    (

    'td, th'

    )

    .forEach

    (

    cell=>

    {
  10. cell.style

    .border

    =

    '1px solid'
  11. }

    )
  12. }

    )
  13. console.log

    (

    container.innerHTML

    )

  14. pdf.html

    (

    `<

    div style=

    'position:absolute;left:0; top:0; bottom:0; height:auto; width:600px; padding:10px;'

    >

    ${

    container.innerHTML

    }

    <

    div>

    `,

    {
  15. callback:

    function

    (

    pdf)

    {
  16. pdf.save

    (

    'html_pdf.pdf'

    )
  17. }

    ,
  18. windowWidth:

    100
  19. }
  20. )
  21. // pdf.save('html_pdf.pdf')
  22. }

  23. window.onload

    =

    function

    (

    )

    {

  24. html_pdf_btn.addEventListener

    (

    'click'

    ,

    function

    (

    e)

    {
  25. e.preventDefault

    (

    )
  26. html_pdf(

    )
  27. }

    )
  28. }

Output

To learn more about how to use JSPDF Library, you can visit their documentation site.

I also provided the working source code zip file that I created for this tutorial. You can download it by clicking the Download Button located after this article's content.

That's it! I hope this Generating PDF File using JavaScript and JSPDF Library Tutorial helps you with what you are looking for and will be useful for your current and future web applications projects.

Happy Coding :)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top