0

I'm searching for a way to convert my page to a PDF document by using jsPDF, but for now I cannot convert any CSS styling. It's just text and no graphics.

This is the code I'm using at the moment

    var specialElementHandlers = {
        '#ignorePDF': function (element,renderer) {
            return true;
        }
    };

    var doc = new jsPDF();

    doc.fromHTML($('#page-with-images').get(0), 20, 20, {'width': 500, 'elementHandlers': specialElementHandlers,});
    doc.save("Test.pdf");

But the result is not what I want, because there is no styling included. How do I fix this problem?

1
  • 1
    You need to apply specific css styling for the pdf. Commented Jul 4, 2016 at 12:54

1 Answer 1

0

Try this.

var pdf = new jsPDF('p', 'pt', 'a4');
pdf.addHTML($("#content"), function() {
  var output = pdf.output("datauristring");
  alert(output);
  //pdf.output("datauri"); //This will output the PDF in a new window
});
div {
  width: 100%;
  background-color: lightblue;
  padding: 15px;
  border: 2px solid black;
}

p {
  background-color: limegreen;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.0.272/jspdf.debug.js"></script>

<body>
  <div id="content">
    <div>
      <div>
        <div>
          <div>
            <p id="to-pdf">HTML content...</p>  
          </div>
        </div>
      </div>
    </div>
  </div>
</body>

Sign up to request clarification or add additional context in comments.

2 Comments

in pdf document only visible elements (screen height - 1000px, but element height - 2000px (with scrolling))
I never really went as deep as that with jsPDF, I just gave you the code to create a PDF from HTML incl. CSS. I guess you should check out their docs, maybe that'll give you a lead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.