1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
/* See: https://godoc.org/github.com/zserge/lorca https://qiita.com/gashirar/items/7ce73c9094b3f777cc9a https://coredake.com/culture/world-paper-size/#toc3 https://developer.mozilla.org/ja/docs/Web/CSS/@page https://ja.wikipedia.org/wiki/%E3%82%A4%E3%83%B3%E3%83%81 */ package main import ( "fmt" "io" "log" "net/url" "os" "path/filepath" "runtime" "github.com/zserge/lorca" ) var ui lorca.UI func main() { workDir, err := filepath.Abs(filepath.Dir(os.Args[0])) // 実行ファイルの絶対パス if err != nil { log.Println("Failed to initialized log, unable to get path of this") } else { // https://qiita.com/74th/items/441ffcab80a6a28f7ee3 fname, err := fileNameWithoutExt(os.Args[0]) if err != nil { log.Println(err) os.Exit(1) } logfilePath := filepath.Join(workDir, fname) + ".log" logfile, err := os.OpenFile(filepath.FromSlash(logfilePath), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) if err != nil { log.Println(err) os.Exit(1) } defer logfile.Close() // io.MultiWriteで 標準出力とファイルの両方を束ねて logの出力先に設定する log.SetOutput(io.MultiWriter(logfile, os.Stdout)) // log.SetFlags(log.Ldate | log.Ltime) log.SetFlags(log.LstdFlags | log.Lmicroseconds) } log.Println("GOOS:", runtime.GOOS) // linux / windows log.Println("GOARCH:", runtime.GOARCH) // amd64 / 386 ui, _ = lorca.New("", "", 480, 320) defer ui.Close() // GoのfuncをJavaScriptで呼び出せるようBind ui.Bind("outputPdfAndPng", outputPdfAndPng) // 今回はurl.PathEscapeでそのままHTMLを配置 // ※下記はHTTPサーバをたててそこから取得する例 // https://github.com/zserge/lorca/tree/master/examples/counter ui.Load("data:text/html," + url.PathEscape(` <!doctype html> <html lang="ja"> <head> <title>output pdf and png</title> </head> <body> <button onclick="outputPdfAndPng()">output pdf and png</button> <div id="content"></div> </body> </html> `)) <-ui.Done() } // FileNameWithoutExt ... // https://qiita.com/KemoKemo/items/d135ddc93e6f87008521 func fileNameWithoutExt(path string) (string, error) { // Fixed with a nice method given by mattn-san path, err := filepath.Abs(path) if err != nil { return "", err } return filepath.Base(path[:len(path)-len(filepath.Ext(path))]), nil } /* // PageA4Width is a width of an A4 page in pixels at 96dpi PageA4Width = 816 // PageA4Height is a height of an A4 page in pixels at 96dpi PageA4Height = 1056 国際インチ 1 インチは正確に 25.4 ミリメートル A4サイズ? 米国規格協会 (ANSI) 216 x 279.5 mm こんなんなん? A4サイズ 日本では普通はこんなん 210 x 297 mm */ // Convert millimeter to paper pixel length, 96dpi func mmToLen(mm float32) int { return (int)(mm * 96 / 25.4) } func outputPdfAndPng() { ui.Eval(`document.getElementById('content').innerHTML="start";`) // url := "https://www.yahoo.co.jp/" url := "data:text/html," + url.PathEscape(` <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" lang="ja"> <link rel="profile" href="http://gmpg.org/xfn/11" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <head> <title>Paper</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; overflow: hidden; } @page { margin: 0; } body { font-family: "VL Pゴシック"; font-size: 10mm; line-height: 1.0; color: #112288; } .page { page-break-after: always; } </style> </head> <body> <div class="page"> ちゅう<br> ちゅう<br> たこ<br> かい<br> な<br> ちゅう<br> ちゅう<br> たこ<br> かい...<br> </div> <div class="page"> ちゅう<br> ちゅう<br> たこ<br> かい<br> な<br> ちゅう<br> ちゅう<br> たこ<br> かい<br> な<br> </div> </body> </html> `) workDir, err := filepath.Abs(filepath.Dir(os.Args[0])) // 実行ファイルの絶対パス basename := filepath.Base(os.Args[0]) // pdf pdfPath := filepath.Join(workDir, basename) + ".pdf" log.Println("Create:", pdfPath) pdf, err := lorca.PDF(url, "", mmToLen(100), mmToLen(100)) if err != nil { fmt.Println(err) return } file, err := os.Create(pdfPath) if err != nil { fmt.Println(err) return } defer file.Close() file.Write(pdf) msg := "output pdf finished" log.Println(msg) ui.Eval(`document.getElementById('content').innerHTML="` + msg + `";`) // png pngPath := filepath.Join(workDir, basename) + ".png" log.Println("Create:", pngPath) png, err := lorca.PNG(url, "", 0, 0, mmToLen(100), mmToLen(100), 0xffffffff, 32) if err != nil { fmt.Println(err) return } file, err = os.Create(pngPath) if err != nil { fmt.Println(err) return } defer file.Close() file.Write(png) msg = "output png finished" log.Println(msg) ui.Eval(`document.getElementById('content').innerHTML="` + msg + `";`) } |