mirror of
https://github.com/Rudi9719/mfprint.git
synced 2026-03-22 05:17:25 +00:00
Initial Commit
This commit is contained in:
94
printer.go
Normal file
94
printer.go
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/johnfercher/maroto/pkg/consts"
|
||||||
|
"github.com/johnfercher/maroto/pkg/pdf"
|
||||||
|
"github.com/johnfercher/maroto/pkg/props"
|
||||||
|
)
|
||||||
|
|
||||||
|
var output string
|
||||||
|
var lastData = time.Now()
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
arguments := os.Args
|
||||||
|
if len(arguments) == 1 {
|
||||||
|
fmt.Println("Please provide host:port.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
CONNECT := arguments[1]
|
||||||
|
c, err := net.Dial("tcp", CONNECT)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
go printWhenDone()
|
||||||
|
scanner := bufio.NewScanner(c)
|
||||||
|
for scanner.Scan() {
|
||||||
|
// Pages are separated via \f or line feed
|
||||||
|
//fmt.Println(scanner.Text())
|
||||||
|
output += fmt.Sprintf("%+v\n", scanner.Text())
|
||||||
|
lastData = time.Now()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func printWhenDone() {
|
||||||
|
for {
|
||||||
|
if time.Since(lastData) > 5*time.Second && output != "" {
|
||||||
|
makePdf()
|
||||||
|
}
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func makePdf() {
|
||||||
|
o := output
|
||||||
|
output = ""
|
||||||
|
jobname := "nil"
|
||||||
|
m := pdf.NewMaroto(consts.Landscape, consts.Letter)
|
||||||
|
scanner := bufio.NewScanner(strings.NewReader(o))
|
||||||
|
format := props.Text{
|
||||||
|
Size: 9,
|
||||||
|
Family: consts.Courier,
|
||||||
|
Top: 0,
|
||||||
|
}
|
||||||
|
firstRun := true
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
if strings.HasPrefix(line, "****A") && strings.Contains(line, "START") {
|
||||||
|
parts := strings.Split(line, " ")
|
||||||
|
jobname = fmt.Sprintf("%+v-JOB%+v", parts[8], parts[6])
|
||||||
|
}
|
||||||
|
page := m.GetCurrentPage()
|
||||||
|
if strings.HasPrefix(line, "\f") && !firstRun {
|
||||||
|
for {
|
||||||
|
if m.GetCurrentPage() != page {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
m.Row(3, func() {
|
||||||
|
m.Col(10, func() {
|
||||||
|
m.Text("\n", format)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
firstRun = false
|
||||||
|
m.Row(3, func() {
|
||||||
|
m.Col(12, func() {
|
||||||
|
m.Text(line, format)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
err := m.OutputFileAndClose(fmt.Sprintf("%+v.pdf", jobname))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error closing pdf\n")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
76
text2pdf/text2pdf.go
Normal file
76
text2pdf/text2pdf.go
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/johnfercher/maroto/pkg/consts"
|
||||||
|
"github.com/johnfercher/maroto/pkg/pdf"
|
||||||
|
"github.com/johnfercher/maroto/pkg/props"
|
||||||
|
)
|
||||||
|
|
||||||
|
var output string
|
||||||
|
var jobname string
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
arguments := os.Args
|
||||||
|
if len(arguments) < 1 {
|
||||||
|
fmt.Println("Please supply a text file to convert.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
jobname = strings.Replace(arguments[0], ".txt", "", -1)
|
||||||
|
outbytes, err := ioutil.ReadFile(arguments[0])
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
output = string(outbytes)
|
||||||
|
makePdf()
|
||||||
|
}
|
||||||
|
|
||||||
|
func makePdf() {
|
||||||
|
o := output
|
||||||
|
output = ""
|
||||||
|
m := pdf.NewMaroto(consts.Landscape, consts.Letter)
|
||||||
|
scanner := bufio.NewScanner(strings.NewReader(o))
|
||||||
|
format := props.Text{
|
||||||
|
Size: 9,
|
||||||
|
Family: consts.Courier,
|
||||||
|
Top: 0,
|
||||||
|
}
|
||||||
|
firstRun := true
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
if strings.HasPrefix(line, "****A") && strings.Contains(line, "START") {
|
||||||
|
parts := strings.Split(line, " ")
|
||||||
|
jobname = fmt.Sprintf("%+v-JOB%+v", parts[8], parts[6])
|
||||||
|
}
|
||||||
|
page := m.GetCurrentPage()
|
||||||
|
if strings.HasPrefix(line, "\f") && !firstRun {
|
||||||
|
for {
|
||||||
|
if m.GetCurrentPage() != page {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
m.Row(3, func() {
|
||||||
|
m.Col(10, func() {
|
||||||
|
m.Text("\n", format)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
firstRun = false
|
||||||
|
m.Row(3, func() {
|
||||||
|
m.Col(12, func() {
|
||||||
|
m.Text(line, format)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
err := m.OutputFileAndClose(fmt.Sprintf("%+v.pdf", jobname))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error closing pdf\n")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user