0

I have a button 'Generate PDF', and I want it to save some div elements as a PDF file. From what I read, I best use JavaScript, but I have zero experience with JS. I already tried third party solutions like html2pdf, pdfmyurl, jsPDF, etc., but I didn't manage to get it to work like I want it.

This is the structure of the HTML file:

<!DOCTYPE html>
<html>
<head>
    <title>This is my title</title>
</head>

<body>

    <!-- HEADER --> 
        <div class="header">
            <img src="../logo/logo_17.png" width="320" height="100" />
        </div> 

    <!-- MENU -->  
        <div class="topnav" id="myTopnav">
            Menu...
        </div>  

    <!-- KOLOMMEN --> 
    <div class="row">
    
        <!-- LINKSE KOLOM -->        
            <div class="prodtitle" id="htmlContent1">Title</div>
            
            <div class="prodbuttons">
                <a class="h2button" href="#">BACK TO LIST</a>
                <button id="generatePDF" class="h2button">generate PDF</button>
            </div>
            
            <div class="prodinfotitle">Info/Description</div>
            
            <div class="prodinfo" id="htmlContent2">
                ...some text
            </div>  
            
            <div class="prodmaterialstitle">Materials/Ingredients</div>
            
            <div class="prodmaterials">
                ...some text 
            </div>
              
            <div class="gallery">            
                ...image 1 (if available)
            </div>
            
            <div class="gallery">
                ...image 2 (if available)
            </div>  
            
        <!-- RECHTSE KOLOM -->         
            <div class="side">
            ... 
            </div>      
    </div>    

    <!-- FOOTER --> 
        <div class="footer">
        ...footer
        </div>
</body>
</html> 

Now this is what I'm looking for:
When you click the 'Generate PDF' button, a PDF file should be created with the following div's: 'header', followed by the 'prodtitle' and then followed by 'prodinfotitle', 'prodinfo', 'prodmaterialstitle', 'prodmaterials' and the picture(s), if they're available. The PDF should then be saved as '[prodtitle].pdf', so that every file has the right title.

Can somebody help me with this piece of code and how to set it up?

Thanks very very much!

Update: Something I tried with my zero experience was this:

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.4/jspdf.min.js"></script>
    
    <script type="text/javascript">
        var doc = new jsPDF();
        var specialElementHandlers = {
            '#editor': function (element, renderer) {
            return true;
            }
        };
 
 
    $('#generatePDF').click(function () {
        doc.fromHTML($('#htmlContent1').html(), 15, 15, {
            'width': 700,
            'elementHandlers': specialElementHandlers
            });
        doc.fromHTML($('#htmlContent2').html(), 15, 15, {
            'width': 700,
            'elementHandlers': specialElementHandlers
            });
        doc.save('sample_file.pdf');
        });
    </script>

It didn't work because I got an empty pdf. I don't even know if the code is correct.

  • What have you tried so far, besides creating the button? – Camelopardalus Jun 20 '22 at 17:15
  • Does this answer your question? [How to make a Convert To PDF button in HTML](https://stackoverflow.com/questions/50656780/how-to-make-a-convert-to-pdf-button-in-html) – Camelopardalus Jun 20 '22 at 17:15
  • Thanks Thomas for the example but that also didn't work. I edited my original post with something I tried. Don't shoot me, because I have no idea what I'm doing in JS. I just copy/pasted it from somewhere on the internet. – RiddleboxBE Jun 24 '22 at 18:20

1 Answers1

0

The danger from cut and paste any old code is it is usually OLD see stackoverflow.com/questions/65177003/… so your script works to build a fresh pdf well but it is not the content on the page just some new overwrite.

Simpler to stick with the HTML you have written before using hybrid methods.

So simply you need to exclude some content for print to pdf but retain the top and bottom.

enter image description here

You need to adapt the button so I suggest along the lines of

<div id="NoPrint">
    <!-- MENU -->  
        <div class="topnav" id="myTopnav">
            Menu...
        </div>  

    <!-- KOLOMMEN --> 
    <div class="row">
    
        <!-- LINKSE KOLOM -->        
            <div class="prodtitle" id="htmlContent1">Title</div>
            
            <div class="prodbuttons">
                
                <button class="h2button"><a class="h2button" href="#">BACK TO LIST</a></button>
                <button id="generatePDF" onclick="print()" >generate PDF Print from Download Page</button>
            </div>
    </div>
 </div NoPrint>

and add in the < head >

<style id="compiled-css" type="text/css">

@media only print {

    #NoPrint {
        display: none;
    }

</style>

The page TITLE drives the offered pdf filename, thus more difficult to adjust <title>[prodtitle].pdf</title> although you could do that via scripting, but that's really a full can of worms in its own right, since the user decides the name to save to.

K J
  • 8,045
  • 3
  • 14
  • 36
  • This got me thinking. I already use something similar. I have a button with window.print() to print the document. I made a css for this so the printed document has it's own style. Can I use this same css to create the pdf and save it and how do I do that? – RiddleboxBE Jun 25 '22 at 18:24
  • Can I force that print button to output it as pdf in stead of sending to a printer or has the user have to select that option himself? – RiddleboxBE Jun 26 '22 at 09:37