commit 8b12ebc95c48b552fdc6e0275315f2adee59975f Author: Gregory Rudolph Date: Sat May 2 14:18:00 2020 -0400 Initial Commit diff --git a/printer.go b/printer.go new file mode 100644 index 0000000..3eb1a62 --- /dev/null +++ b/printer.go @@ -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 + } +} diff --git a/text2pdf/text2pdf.go b/text2pdf/text2pdf.go new file mode 100644 index 0000000..2511307 --- /dev/null +++ b/text2pdf/text2pdf.go @@ -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 + } +}