├── .gitignore ├── LICENSE ├── PdfFilter.dll ├── PdfPreview.dll ├── README.md ├── SumatraPDF.exe ├── go.mod ├── go.sum ├── libmupdf.dll └── main.go /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | 4 | # local env files 5 | .env.local 6 | .env.*.local 7 | 8 | # Log files 9 | npm-debug.log* 10 | yarn-debug.log* 11 | yarn-error.log* 12 | 13 | # Editor directories and files 14 | .idea 15 | .vscode 16 | *.suo 17 | *.ntvs* 18 | *.njsproj 19 | *.sln 20 | *.sw? 21 | 22 | dist 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 gloomysw 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /PdfFilter.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Serendipity-sw/go-http-printer/c70bf1424e6b8b57b006310d26f99a99e1045c5b/PdfFilter.dll -------------------------------------------------------------------------------- /PdfPreview.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Serendipity-sw/go-http-printer/c70bf1424e6b8b57b006310d26f99a99e1045c5b/PdfPreview.dll -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # golang调用windows程序实现pdf调用打印机 2 | 3 | # 采用http调用形式 4 | 5 | http://127.0.0.1:1201/Print?file=filePath 6 | 7 | filePath 需要网络上的文件地址 8 | -------------------------------------------------------------------------------- /SumatraPDF.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Serendipity-sw/go-http-printer/c70bf1424e6b8b57b006310d26f99a99e1045c5b/SumatraPDF.exe -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module go-http-printer 2 | 3 | go 1.15 4 | 5 | require ( 6 | github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c // indirect 7 | github.com/dsnet/compress v0.0.1 // indirect 8 | github.com/garyburd/redigo v1.6.2 // indirect 9 | github.com/gin-gonic/gin v1.6.3 10 | github.com/go-sql-driver/mysql v1.5.0 // indirect 11 | github.com/gobwas/glob v0.2.3 // indirect 12 | github.com/guotie/days v0.0.0-20170223003041-4e080d546dc9 // indirect 13 | github.com/guotie/deferinit v0.0.0-20160907060517-7ecd28352af0 14 | github.com/howeyc/fsnotify v0.9.0 // indirect 15 | github.com/jlaffaye/ftp v0.0.0-20201112195030-9aae4d151126 // indirect 16 | github.com/klauspost/pgzip v1.2.5 // indirect 17 | github.com/lib/pq v1.9.0 // indirect 18 | github.com/pkg/sftp v1.12.0 // indirect 19 | github.com/smtc/glog v0.0.0-20170223054335-a5061591d8b8 20 | github.com/swgloomy/gutil v0.0.0-20181224073202-71ca5d75b69f 21 | github.com/tealeg/xlsx v1.0.5 // indirect 22 | github.com/ulikunitz/xz v0.5.8 // indirect 23 | golang.org/x/crypto v0.0.0-20201217014255-9d1352758620 // indirect 24 | ) 25 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c h1:aprLqMn7gSPT+vdDSl+/E6NLEuArwD/J7IWd8bJt5lQ= 2 | github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c/go.mod h1:Ie6SubJv/NTO9Q0UBH0QCl3Ve50lu9hjbi5YJUw03TE= 3 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 4 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 5 | github.com/dsnet/compress v0.0.1 h1:PlZu0n3Tuv04TzpfPbrnI0HW/YwodEXDS+oPKahKF0Q= 6 | github.com/dsnet/compress v0.0.1/go.mod h1:Aw8dCMJ7RioblQeTqt88akK31OvO8Dhf5JflhBbQEHo= 7 | github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY= 8 | github.com/garyburd/redigo v1.6.2 h1:yE/pwKCrbLpLpQICzYTeZ7JsTA/C53wFTJHaEtRqniM= 9 | github.com/garyburd/redigo v1.6.2/go.mod h1:NR3MbYisc3/PwhQ00EMzDiPmrwpPxAn5GI05/YaO1SY= 10 | github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= 11 | github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= 12 | github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14= 13 | github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M= 14 | github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= 15 | github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= 16 | github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= 17 | github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= 18 | github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= 19 | github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY= 20 | github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI= 21 | github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= 22 | github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= 23 | github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= 24 | github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8= 25 | github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I= 26 | github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= 27 | github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= 28 | github.com/guotie/days v0.0.0-20170223003041-4e080d546dc9 h1:3NlkEv6GxFZnMm99gBDwieblra20JbMCUPDiP4Y/l6g= 29 | github.com/guotie/days v0.0.0-20170223003041-4e080d546dc9/go.mod h1:WSyIpwX/1YlVAHx/xgDrfyDPzx4r39wd+AGw5JNXxuk= 30 | github.com/guotie/deferinit v0.0.0-20160907060517-7ecd28352af0 h1:Iy5AacZpD6vUjXiEau0/9/cPExSYiV2/wldytMnNp2Q= 31 | github.com/guotie/deferinit v0.0.0-20160907060517-7ecd28352af0/go.mod h1:TVdYVTdxo9pFZBOPW4DzSDcfljs6eVz4JG7jWuIIsZE= 32 | github.com/howeyc/fsnotify v0.9.0 h1:0gtV5JmOKH4A8SsFxG2BczSeXWWPvcMT0euZt5gDAxY= 33 | github.com/howeyc/fsnotify v0.9.0/go.mod h1:41HzSPxBGeFRQKEEwgh49TRw/nKBsYZ2cF1OzPjSJsA= 34 | github.com/jlaffaye/ftp v0.0.0-20201112195030-9aae4d151126 h1:ly2C51IMpCCV8RpTDRXgzG/L9iZXb8ePEixaew/HwBs= 35 | github.com/jlaffaye/ftp v0.0.0-20201112195030-9aae4d151126/go.mod h1:2lmrmq866uF2tnje75wQHzmPXhmSWUt7Gyx2vgK1RCU= 36 | github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= 37 | github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= 38 | github.com/klauspost/compress v1.4.1 h1:8VMb5+0wMgdBykOV96DwNwKFQ+WTI4pzYURP99CcB9E= 39 | github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= 40 | github.com/klauspost/cpuid v1.2.0 h1:NMpwD2G9JSFOE1/TJjGSo5zG7Yb2bTe7eq1jH+irmeE= 41 | github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= 42 | github.com/klauspost/pgzip v1.2.5 h1:qnWYvvKqedOF2ulHpMG72XQol4ILEJ8k2wwRl/Km8oE= 43 | github.com/klauspost/pgzip v1.2.5/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs= 44 | github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8= 45 | github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= 46 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= 47 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 48 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 49 | github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= 50 | github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= 51 | github.com/lib/pq v1.9.0 h1:L8nSXQQzAYByakOFMTwpjRoHsMJklur4Gi59b6VivR8= 52 | github.com/lib/pq v1.9.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= 53 | github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= 54 | github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= 55 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= 56 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 57 | github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= 58 | github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= 59 | github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= 60 | github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 61 | github.com/pkg/sftp v1.12.0 h1:/f3b24xrDhkhddlaobPe2JgBqfdt+gC/NYl0QY9IOuI= 62 | github.com/pkg/sftp v1.12.0/go.mod h1:fUqqXB5vEgVCZ131L+9say31RAri6aF6KDViawhxKK8= 63 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 64 | github.com/smtc/glog v0.0.0-20170223054335-a5061591d8b8 h1:wOmhTebalcoFiJkE7kMXb9aO81umTeBS15wPUZJoCDQ= 65 | github.com/smtc/glog v0.0.0-20170223054335-a5061591d8b8/go.mod h1:Ft0Wy+HrchsUrBeEE7powRNykPGMDv7UODhvgylFdXU= 66 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 67 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 68 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 69 | github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 70 | github.com/swgloomy/gutil v0.0.0-20181224073202-71ca5d75b69f h1:87wEuyUGnbamhwfreAnfmsg/yT8sXKAMNDnBDflganU= 71 | github.com/swgloomy/gutil v0.0.0-20181224073202-71ca5d75b69f/go.mod h1:hWZeeTUOGSS3WR1E3HQQ7X6fFqHzy/Iv+VskVCn7RZk= 72 | github.com/tealeg/xlsx v1.0.5 h1:+f8oFmvY8Gw1iUXzPk+kz+4GpbDZPK1FhPiQRd+ypgE= 73 | github.com/tealeg/xlsx v1.0.5/go.mod h1:btRS8dz54TDnvKNosuAqxrM1QgN1udgk9O34bDCnORM= 74 | github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= 75 | github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= 76 | github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= 77 | github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= 78 | github.com/ulikunitz/xz v0.5.6/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8= 79 | github.com/ulikunitz/xz v0.5.8 h1:ERv8V6GKqVi23rgu5cj9pVfVzJbOqAY2Ntl88O6c2nQ= 80 | github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= 81 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 82 | golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 83 | golang.org/x/crypto v0.0.0-20201217014255-9d1352758620 h1:3wPMTskHO3+O6jqTEXyFcsnuxMQOqYSaHsDxcbUXpqA= 84 | golang.org/x/crypto v0.0.0-20201217014255-9d1352758620/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= 85 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 86 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 87 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 88 | golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 89 | golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg= 90 | golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 91 | golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= 92 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 93 | golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= 94 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 95 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 96 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 97 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 98 | gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= 99 | gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 100 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 101 | -------------------------------------------------------------------------------- /libmupdf.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Serendipity-sw/go-http-printer/c70bf1424e6b8b57b006310d26f99a99e1045c5b/libmupdf.dll -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/gin-gonic/gin" 5 | "github.com/guotie/deferinit" 6 | "github.com/smtc/glog" 7 | "github.com/swgloomy/gutil" 8 | "io/ioutil" 9 | "net/http" 10 | "os" 11 | "os/exec" 12 | "os/signal" 13 | "runtime" 14 | "syscall" 15 | ) 16 | 17 | var ( 18 | port = ":1201" 19 | pidPath = "./httpPrint.pid" //pid文件 20 | logDir = "./logs" 21 | rt *gin.Engine 22 | ) 23 | 24 | func main() { 25 | //if gutil.CheckPid(pidPath) { 26 | // return 27 | //} 28 | gutil.LogInit(true, logDir) 29 | serverStart() 30 | 31 | rt = gin.Default() 32 | rt.Use(Cors()) 33 | router(rt) 34 | 35 | go func() { 36 | err := rt.Run(port) 37 | if err != nil { 38 | glog.Error("main gin run err! err: %s \n", err.Error()) 39 | } 40 | }() 41 | 42 | c := make(chan os.Signal, 1) 43 | //gutil.WritePid(pidPath) 44 | // 信号处理 45 | signal.Notify(c, os.Interrupt, os.Kill, syscall.SIGTERM) 46 | // 等待信号 47 | <-c 48 | serverExit() 49 | //gutil.RmPidFile(pidPath) 50 | os.Exit(0) 51 | } 52 | 53 | func router(r *gin.Engine) { 54 | g := &r.RouterGroup 55 | g.GET("/", func(c *gin.Context) { c.String(http.StatusOK, "ok") }) 56 | g.GET("/Print", printPdf) 57 | } 58 | 59 | func printPdf(c *gin.Context) { 60 | fileUrl := c.Query("file") 61 | go fileDown(fileUrl) 62 | c.String(http.StatusOK, "打印文件处理中!") 63 | } 64 | 65 | func fileDown(fileUrl string) { 66 | resp, err := http.Get(fileUrl) 67 | if err != nil { 68 | glog.Error("router http downLoad,err: %s \n", err) 69 | return 70 | } 71 | defer func() { 72 | err = resp.Body.Close() 73 | if err != nil { 74 | glog.Error("fileDown resp body close err! err: %s \n", err.Error()) 75 | return 76 | } 77 | }() 78 | 79 | data, err := ioutil.ReadAll(resp.Body) 80 | if err != nil { 81 | glog.Error("fileDown http read err! err: %s \n", err.Error()) 82 | return 83 | } 84 | pdfPath := "./gloomy.pdf" 85 | bo, err := gutil.PathExists(pdfPath) 86 | if err != nil { 87 | glog.Error("fileDown path exists file run err! err: %s \n", err.Error()) 88 | } 89 | if bo { 90 | err = os.Remove(pdfPath) 91 | if err != nil { 92 | glog.Error("fileDown remove file err! err: %s \n", err.Error()) 93 | return 94 | } 95 | } 96 | err = ioutil.WriteFile(pdfPath, data, 0644) 97 | if err != nil { 98 | glog.Error("fileDown write file err! err: %s \n", err.Error()) 99 | return 100 | } 101 | cmd := exec.Command("./SumatraPDF.exe", "-print-to-default", pdfPath) 102 | stdout, err := cmd.StdoutPipe() 103 | if err != nil { 104 | glog.Error("fileDown StdoutPipe run err! err: %s \n", err.Error()) 105 | return 106 | } 107 | defer func() { 108 | err = stdout.Close() 109 | if err != nil { 110 | glog.Error("fileDown stdout close err! err: %s \n", err.Error()) 111 | return 112 | } 113 | }() 114 | 115 | err = cmd.Start() 116 | if err != nil { 117 | glog.Error("fileDown cmd start err! err: %s \n", err.Error()) 118 | return 119 | } 120 | opBytes, err := ioutil.ReadAll(stdout) 121 | if err != nil { 122 | glog.Error("fileDown cmd read result err! err: %s \n", err.Error()) 123 | return 124 | } 125 | glog.Info("fileDown cmd start success! result: %s \n", string(opBytes)) 126 | } 127 | 128 | func Cors() gin.HandlerFunc { 129 | return func(c *gin.Context) { 130 | method := c.Request.Method 131 | 132 | c.Header("Access-Control-Allow-Origin", "*") 133 | c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token") 134 | c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS") 135 | c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type") 136 | c.Header("Access-Control-Allow-Credentials", "true") 137 | 138 | //放行所有OPTIONS方法 139 | if method == "OPTIONS" { 140 | c.AbortWithStatus(http.StatusNoContent) 141 | } 142 | // 处理请求 143 | c.Next() 144 | } 145 | } 146 | 147 | func serverStart() { 148 | //初始化所有go文件中的init内方法 149 | deferinit.InitAll() 150 | glog.Info("init all module successfully \n") 151 | 152 | //设置多CPU运行 153 | runtime.GOMAXPROCS(runtime.NumCPU()) 154 | glog.Info("set many cpu successfully \n") 155 | 156 | //启动所有go文件中的init方法 157 | deferinit.RunRoutines() 158 | glog.Info("init all run successfully \n") 159 | } 160 | 161 | func serverExit() { 162 | // 结束所有go routine 163 | deferinit.StopRoutines() 164 | glog.Info("stop routine successfully.\n") 165 | deferinit.FiniAll() 166 | glog.Info("fini all modules successfully.\n") 167 | 168 | glog.Close() 169 | } 170 | --------------------------------------------------------------------------------