Mindinventory / Golang-HTML-TO-PDF-Converter

Golang HTML to PDF Converter
https://www.mindinventory.com/golang-development.php
MIT License
362 stars 87 forks source link

Multi-page support? #10

Closed MrWildanMD closed 10 months ago

MrWildanMD commented 11 months ago

Lets say i want to convert multiple page or lets say pagination...how its possible? thanks

dangkaka commented 10 months ago

I have a same question, any updates @MrWildanMD ?

MrWildanMD commented 10 months ago

not yet....u can use fpdf but it need more line of code to implement that thing...and bit confusing

kaivalya-mi commented 10 months ago

Thanks for bringing this to my attention,@MrWildanMD I understand your concern. It seems that the provided demo code in main.go and sample.html is currently designed for creating a single entry in the table.


//changes in models main.go
type TemplateData struct {
    Title       string
    Description string
    Pages       []Page
}
type Page struct {
    PageNumber int
    Entries    []Entries
}
type Entries struct {
    Company string
    Contact string
    Country string
}
<!-- changes in template in sample.html-->
 <body>
    {{ range .Pages }}
    <h3>{{ $.Title }} - Page {{ .PageNumber }}</h3>
    <table>
      <thead>
        <tr>
          <th>Company</th>
          <th>Contact</th>
          <th>Country</th>
        </tr>
      </thead>
      <tbody>
        {{ range .Entries }}
        <tr>
          <td>{{ .Company }}</td>
          <td>{{ .Contact }}</td>
          <td>{{ .Country }}</td>
        </tr>
        {{ end }}
      </tbody>
    </table>
    {{ if ne .PageNumber (len $.Pages) }}
    <div class="page-break"></div>
    {{ end }} {{ end }}
  </body>
MrWildanMD commented 10 months ago

Thanks for bringing this to my attention,@MrWildanMD I understand your concern. It seems that the provided demo code in main.go and sample.html is currently designed for creating a single entry in the table.

  • The package is designed to handle custom Golang and HTML code, allowing the creation of PDFs using HTML. However, to achieve the specific functionality you're seeking, you'll need to make adjustments to the code related to the struct models and fine-tune the structure of the sample.html template.
//changes in models main.go
type TemplateData struct {
  Title       string
  Description string
  Pages       []Page
}
type Page struct {
  PageNumber int
  Entries    []Entries
}
type Entries struct {
  Company string
  Contact string
  Country string
}
<!-- changes in template in sample.html-->
 <body>
    {{ range .Pages }}
    <h3>{{ $.Title }} - Page {{ .PageNumber }}</h3>
    <table>
      <thead>
        <tr>
          <th>Company</th>
          <th>Contact</th>
          <th>Country</th>
        </tr>
      </thead>
      <tbody>
        {{ range .Entries }}
        <tr>
          <td>{{ .Company }}</td>
          <td>{{ .Contact }}</td>
          <td>{{ .Country }}</td>
        </tr>
        {{ end }}
      </tbody>
    </table>
    {{ if ne .PageNumber (len $.Pages) }}
    <div class="page-break"></div>
    {{ end }} {{ end }}
  </body>
  • Additionally, consider implementing custom pagination. By incorporating these adjustments, you can handle multiple entries in the table, and it will enable the creation of file with multiple pages.
  • It's important to note that, unlike libraries such as fpdf, the primary purpose of this package is to streamline the conversion process using the wkhtmltopdf package. The focus is on simplifying the conversion from Golang and HTML to PDF.

Great answer, thank you, i'm heading out to try it