Go의 PDF 보고서용 HTML

개발자로서 때때로 내 응용 프로그램에 대한 PDF 보고서를 만들어야 합니다.

프로그래밍으로 완전히 생성하는 것은 번거로울 수 있으며 모든 라이브러리는 조금씩 다릅니다. 마지막으로 디자이너가 원하는 대로 사물을 만드는 것은 어려울 수 있습니다.

많은 시간을 들이지 않고도 디자인처럼 보이게 만들 수 있다면 좋지 않을까요?

디자이너와 프론트엔더는 일반적으로 HTML과 CSS를 할 수 있으므로 HTML을 활용하는 것이 좋습니다.

그러나 웹 사이트는 일반적으로 인쇄할 때 보기 좋지 않으며 여러 페이지용으로 설계되지 않았습니다.

우리는 위의 모든 문제를 해결하는 솔루션을 찾았습니다.

UniPDF와 결합된 UniHTML을 만나보세요



UniHTMLUniPDF 의 플래그십 라이브러리 중 하나인 UniDoc 의 새 플러그인입니다.

회로도에 따라 Go 드라이버가 포함된 컨테이너 기반 솔루션입니다.



Docker 이미지는 Docker 허브에 publicly available 있습니다.

UniPDF 작성자 패키지는 creating flexible PDF reportsinvoices 을 활성화합니다. UniHTML 컨테이너 기반 모듈에는 유연한 웹 렌더링 엔진이 있으며 UniPDF와 함께 UniPDF 보고서 생성에 전체 HTML 지원을 추가하는 기능을 함께 제공합니다.

사용해보기



시도해 봅시다.

1단계. 무료 측정 API 키를 생성합니다.



https://cloud.unidoc.io에 계정을 등록하고 UI에서 측정 API 키를 생성하면 됩니다.

이에 대한 단계별 지침은 다음을 참조하십시오.
  • How to sign up for UniCloud
  • How to generate a metered API key

  • 2단계. 실행 중인 UniHTML 컨테이너 가져오기




    $ docker run -p 8080:8080 -e UNIDOC_METERED_API_KEY=mymeteredkey unidoccloud/unihtml
    Unable to find image 'unidoccloud/unihtml:latest' locally
    latest: Pulling from unidoccloud/unihtml
    6e640006d1cd: Pull complete
    1a3def68b0c4: Pull complete
    5b1718db67b4: Pull complete
    8d4c41b870b6: Pull complete
    b1a4436c2bab: Pull complete
    3c3af5a4fff5: Pull complete
    29863d0ede88: Pull complete
    Digest: sha256:c1c69af194358179d836a648f07f71af07ed0c968938abe3a3e2550e49980728
    Status: Downloaded newer image for unidoccloud/unihtml:latest
    [INFO]  server.go:173 Listening private API on: :8081
    [INFO]  server.go:164 Listening public API on: :8080
    


    3단계. 예제 실행.



    블로그 게시물에서 영감을 받아 HTML 표가 있는 PDF 보고서를 설명하기 위해 다음 HTML 파일을 함께 만들었습니다.

    sample.html




    <html>
    <head>
    <style>
    
    .styled-table {
        border-collapse: collapse;
        margin: 25px 0;
        font-size: 0.9em;
        font-family: sans-serif;
        min-width: 400px;
        box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
    }
    
    .styled-table thead tr {
        background-color: #009879;
        color: #ffffff;
        text-align: left;
    }
    
    .styled-table th,
    .styled-table td {
        padding: 12px 15px;
    }
    
    
    .styled-table tbody tr {
        border-bottom: 1px solid #dddddd;
    }
    
    .styled-table tbody tr:nth-of-type(even) {
        background-color: #f3f3f3;
    }
    
    .styled-table tbody tr:last-of-type {
        border-bottom: 2px solid #009879;
    }
    
    
    .styled-table tbody tr.active-row {
        font-weight: bold;
        color: #009879;
    }
    
    </style>
    </head>
    
    <table class="styled-table">
        <thead>
            <tr>
                <th>Name</th>
                <th>Points</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Dom</td>
                <td>6000</td>
            </tr>
            <tr class="active-row">
                <td>Melissa</td>
                <td>5150</td>
            </tr>
            <!-- and so on... -->
        </tbody>
    </table>
    
    </html>
    


    example.go




    package main
    
    import (
        "fmt"
        "os"
    
        "github.com/unidoc/unihtml"
        "github.com/unidoc/unipdf/v3/common/license"
        "github.com/unidoc/unipdf/v3/creator"
    )
    
    func main() {
        // Set the UniDoc license.
        if err := license.SetMeteredKey("my api key goes here"); err != nil {
            fmt.Printf("Err: setting metered key failed: %v\n", err)
            os.Exit(1)
        }
    
        // Establish connection with the UniHTML Server.
        if err := unihtml.Connect(":8080"); err != nil {
            fmt.Printf("Err:  Connect failed: %v\n", err)
            os.Exit(1)
        }
    
        // Get new PDF Creator.
        c := creator.New()
    
        // AddTOC enables Table of Contents generation.
        c.AddTOC = true
    
        chapter := c.NewChapter("Points")
    
        // Read the content of the sample.html file and load it to the conversion.
        htmlDocument, err := unihtml.NewDocument("sample.html")
        if err != nil {
            fmt.Printf("Err: NewDocument failed: %v\n", err)
            os.Exit(1)
        }
    
        // Draw the html document file in the context of the creator.
        if err = chapter.Add(htmlDocument); err != nil {
            fmt.Printf("Err: Draw failed: %v\n", err)
            os.Exit(1)
        }
    
        if err = c.Draw(chapter); err != nil {
            fmt.Printf("Err: Draw failed: %v\n", err)
            os.Exit(1)
        }
    
    
        // Write the result file to PDF.
        if err = c.WriteToFile("sample.pdf"); err != nil {
            fmt.Printf("Err: %v\n", err)
            os.Exit(1)
        }
    }
    


    결과



    결과 실행:

    $ go run example.go
    


    생성sample.pdf은 다음과 같습니다.


    목차도 있으니 참고하세요


    PDF의 각 장에 연결되는 책갈피.

    머리글과 바닥글도 쉽게 만들 수 있습니다.

    결론



    UniHTML은 전체 렌더링 엔진을 사용하여 UniPDF에서 HTML을 PDF 보고서로 쉽게 생성할 수 있도록 합니다. 이렇게 하면 이미 HTML 디자인이 있고 웹 사이트의 일반 PDF 인쇄로는 충분하지 않은 전문 PDF 보고서를 추가해야 하는 팀에서 PDF 보고서 생성 프로세스를 정말 쉽게 만들 수 있습니다.

    좋은 웹페이지 즐겨찾기