feat: build scripts & src...

This commit is contained in:
Barold 2020-12-11 22:49:43 +02:00
parent f20fa17df3
commit d1e89f060f
3 changed files with 123 additions and 12 deletions

View File

@ -5,8 +5,10 @@ import (
"encoding/json" "encoding/json"
"flag" "flag"
"fmt" "fmt"
wkhtml "github.com/SebastiaanKlippert/go-wkhtmltopdf"
"github.com/pdfcpu/pdfcpu/pkg/api" "github.com/pdfcpu/pdfcpu/pkg/api"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
@ -32,6 +34,7 @@ func main() {
passwordPtr := flag.String("p", "", "Zinio Password") passwordPtr := flag.String("p", "", "Zinio Password")
chromePtr := flag.String("c", "google-chrome", "Chrome executable") chromePtr := flag.String("c", "google-chrome", "Chrome executable")
zinioHostPtr := flag.String("e", "api-sec.ziniopro.com", "Zinio Host (Excluding port and URI Scheme). Known: `api-sec`, `api-sec-2`") zinioHostPtr := flag.String("e", "api-sec.ziniopro.com", "Zinio Host (Excluding port and URI Scheme). Known: `api-sec`, `api-sec-2`")
exportUsingWKHTML := flag.String("wkhtml", "false", "Use WKHTML instead of Chrome to generate PDF (false by default)")
flag.Parse() flag.Parse()
@ -45,7 +48,7 @@ func main() {
fmt.Println("Found " + strconv.Itoa(len(issues.Data)) + " issues in library.") fmt.Println("Found " + strconv.Itoa(len(issues.Data)) + " issues in library.")
fmt.Println("Loading HTML template") fmt.Println("Loading HTML template")
defaultTemplate := "<html><head><!--<style>@media all{@page{margin: 0px;}body{margin-top: 0cm; margin-left:auto;}}</style>--><style>html, body{width: fit-content;height: fit-content;margin: 0px;padding: 0px;}</style><style id=page_style>@page{size: 100px 100px ; margin : 0px}</style></head><body><object type=\"image/svg+xml\" data=\"SVG_PATH\" ></object><script>window.onload=fixpage;function fixpage(){renderBlock=document.getElementsByTagName(\"html\")[0]; renderBlockInfo=window.getComputedStyle(renderBlock) // fix chrome page bug fixHeight=parseInt(renderBlockInfo.height) + 1 + \"px\" pageCss=`@page{size: \\${renderBlockInfo.width}\\${fixHeight}; margin:0;}` document.getElementById(\"page_style\").innerHTML=pageCss}</script></body></html>" defaultTemplate := GetDefaultTemplate()
template, _ := ioutil.ReadFile("template.html") template, _ := ioutil.ReadFile("template.html")
if template == nil || len(template) == 0 { if template == nil || len(template) == 0 {
@ -71,6 +74,7 @@ func main() {
fmt.Println("Issue already found: " + issue.Publication.Name + " - " + issue.Name) fmt.Println("Issue already found: " + issue.Publication.Name + " - " + issue.Name)
continue continue
} }
fmt.Println("Downloading issue: " + issue.Publication.Name + " - " + issue.Name)
pages := GetPages(loginToken, issue, initialToken, *zinioHostPtr) pages := GetPages(loginToken, issue, initialToken, *zinioHostPtr)
@ -82,26 +86,72 @@ func main() {
pathString := issuePath + "_" + pages.Data[i].Index pathString := issuePath + "_" + pages.Data[i].Index
htmldata := strings.Replace(string(template), "SVG_PATH", pages.Data[i].Source, -1) resp, err := http.Get(pages.Data[i].Source)
//write html file, embedding svg // handle the error if there is one
ioutil.WriteFile(pathString+".html", []byte(htmldata), 0644) if err != nil {
panic(err)
}
// do this now so it won't be forgotten
defer resp.Body.Close()
// reads html as a slice of bytes
html, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
// show the HTML code as a string %s
htmldata := strings.Replace(string(template), "SVG_PATH", string(html), -1)
//convert to pdf //convert to pdf
cmd := exec.Command(*chromePtr, "--headless", "--disable-gpu", "--print-to-pdf="+pathString+".pdf", "--no-margins", pathString+".html") if strings.ToLower(*exportUsingWKHTML) == "true" {
pdfg, err := wkhtml.NewPDFGenerator()
if err != nil {
return
}
pdfg.MarginBottom.Set(0)
pdfg.MarginTop.Set(0)
pdfg.MarginLeft.Set(0)
pdfg.MarginRight.Set(0)
pdfg.NoOutline.Set(true)
//pdfg.PageSize.Set(wkhtml.PageSizeCustom)
pdfg.AddPage(wkhtml.NewPageReader(strings.NewReader(htmldata)))
// Create PDF document in internal buffer
err = pdfg.Create()
if err != nil {
log.Fatal(err)
}
//Your Pdf Name
err = pdfg.WriteFile(pathString + ".pdf")
if err != nil {
log.Fatal(err)
}
} else {
//write html file, embedding svg
ioutil.WriteFile(pathString+".html", []byte(htmldata), 0644)
cmd := exec.Command(*chromePtr, "--headless", "--disable-gpu", "--print-to-pdf="+pathString+".pdf", "--no-margins", pathString+".html")
fmt.Println(cmd.Args)
err := cmd.Run()
if err != nil {
fmt.Printf("cmd.Run() failed with %s\n. You should retry this page.", err)
}
fmt.Println(cmd.Args)
err := cmd.Run()
if err != nil {
fmt.Printf("cmd.Run() failed with %s\n. You should retry this page.", err)
} }
_ = os.Remove(pathString + ".html") _ = os.Remove(pathString + ".html")
_ = os.Remove(pathString + ".svg") _ = os.Remove(pathString + ".svg")
filenames = append(filenames, pathString+".pdf")
}
for i := range filenames {
//remove last page //remove last page
err = retry(5, 2*time.Second, func() (err error) { err = retry(5, 2*time.Second, func() (err error) {
err = api.RemovePagesFile(pathString+".pdf", "", []string{"2-"}, nil) err = api.RemovePagesFile(filenames[i], "", []string{"2-"}, nil)
if err != nil { if err != nil {
fmt.Printf("Removing extra pages failed with %s\n.", err) fmt.Printf("Removing extra pages failed with %s\n.", err)
@ -111,8 +161,6 @@ func main() {
return return
}) })
filenames = append(filenames, pathString+".pdf")
} }
_ = api.MergeCreateFile(filenames, completeName, nil) _ = api.MergeCreateFile(filenames, completeName, nil)
@ -266,3 +314,53 @@ func retry(attempts int, sleep time.Duration, f func() error) (err error) {
} }
return fmt.Errorf("after %d attempts, last error: %s", attempts, err) return fmt.Errorf("after %d attempts, last error: %s", attempts, err)
} }
func GetDefaultTemplate() string {
return `<html>
<head>
<!--<style>
@media all {
@page { margin: 0px; }
body { margin-top: 0cm;
margin-left:auto;
}
}
</style>-->
<style>
html, body {
width: fit-content;
height: fit-content;
margin: 0px;
padding: 0px;
}
</style>
<style id=page_style>
@page { size: 100px 100px ; margin : 0px }
</style>
</head>
<body>
<object type="image/svg+xml" data="SVG_PATH" ></object>
<script>
window.onload = fixpage;
function fixpage() {
renderBlock = document.getElementsByTagName("html")[0];
renderBlockInfo = window.getComputedStyle(renderBlock)
// fix chrome page bug
fixHeight = parseInt(renderBlockInfo.height) + 1 + "px"
pageCss = '@page { size: \${renderBlockInfo.width} \${fixHeight} ; margin:0;}'
document.getElementById("page_style").innerHTML = pageCss
}
</script>
</body>
</html>`
}

View File

@ -20,3 +20,6 @@ Each page is available as an SVG, which is then injected into an HTML page (base
google-chrome is then used to print the page to PDF, and all pages are combined into a single PDF. google-chrome is then used to print the page to PDF, and all pages are combined into a single PDF.
###Building
Build for linux & windows on windows using the pwershell script in buildscripts

10
buildscripts/windows.ps1 Normal file
View File

@ -0,0 +1,10 @@
$env:GOOS = 'windows'
$env:GOARCH = 'amd64'
cd ..
go build -o built/Zinigo_Windows_x64.exe Grabazine.go
cd buildscripts
$env:GOOS = 'linux'
$env:GOARCH = 'amd64'
cd ..
go build -o built/Zinigo_Linux_AMD64 Grabazine.go
cd buildscripts