Добавлено: конвертация в pdf

This commit is contained in:
far-galaxy 2023-03-25 21:19:57 +04:00
parent ea69e5dd2d
commit 88e3aa346b
3 changed files with 100 additions and 13 deletions

2
go.mod
View File

@ -10,9 +10,11 @@ require (
)
require (
github.com/SebastiaanKlippert/go-wkhtmltopdf v1.9.0 // indirect
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/deckarep/golang-set/v2 v2.3.0 // indirect
github.com/icza/gox v0.0.0-20230117093757-93f961aa2755 // indirect
github.com/pdfcpu/pdfcpu v0.4.0 // indirect
golang.org/x/net v0.7.0 // indirect
)

4
go.sum
View File

@ -7,6 +7,8 @@ github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAcwmWM=
github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ=
github.com/SebastiaanKlippert/go-wkhtmltopdf v1.9.0 h1:DNrExYwvyyI404SxdUCCANAj9TwnGjRfa3cYFMNY1AU=
github.com/SebastiaanKlippert/go-wkhtmltopdf v1.9.0/go.mod h1:SQq4xfIdvf6WYKSDxAJc+xOJdolt+/bc1jnQKMtPMvQ=
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
@ -278,6 +280,8 @@ github.com/openzipkin/zipkin-go v0.2.2/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnh
github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIwwtUjcrb0b5/5kLM=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
github.com/pdfcpu/pdfcpu v0.4.0 h1:381iGNvMeLP+GFqIAqgd0LSj36AsK3JH4UTaF6D5jRc=
github.com/pdfcpu/pdfcpu v0.4.0/go.mod h1:9NDeS6hrCheauxw6YUlzgL/q6At2+PMzUKyFcfUzLLY=
github.com/performancecopilot/speed v3.0.0+incompatible/go.mod h1:/CLtqpZ5gBg1M9iaPbIdPPGyKcA8hKdoy6hAWba7Yac=
github.com/pierrec/lz4 v1.0.2-0.20190131084431-473cd7ce01a1/go.mod h1:3/3N9NVKO0jef7pBehbT1qWhCMrIgbYNnFAZCqQ5LRc=
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=

View File

@ -2,11 +2,16 @@ package tg
import (
"fmt"
"io/ioutil"
"log"
"math"
"os"
"strings"
"time"
"git.l9labs.ru/anufriev.g.a/l9_stud_bot/modules/database"
"git.l9labs.ru/anufriev.g.a/l9_stud_bot/modules/ssau_parser"
wkhtml "github.com/SebastiaanKlippert/go-wkhtmltopdf"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/icza/gox/timex"
)
@ -88,23 +93,84 @@ func (bot *Bot) GetWeekSummary(shedules []database.ShedulesInUser, dw int, isPer
shedule[p[0].NumInShedule][day] = p
}
bot.CreateHTMLShedule(week, shedule, dates, times)
html := bot.CreateHTMLShedule(week, shedule, dates, times)
f, _ := os.Create("sh.html")
defer f.Close()
f.WriteString(html)
wkhtml.SetPath("C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe")
pdfg, err := wkhtml.NewPDFGenerator()
if err != nil {
log.Fatal(err)
}
pdfg.Dpi.Set(300)
pdfg.MarginBottom.Set(0)
pdfg.MarginTop.Set(0)
pdfg.MarginLeft.Set(0)
pdfg.MarginRight.Set(0)
pdfg.Orientation.Set(wkhtml.OrientationLandscape)
pdfg.PageSize.Set(wkhtml.PageSizeA4)
pdfg.AddPage(wkhtml.NewPageReader(strings.NewReader(html)))
err = pdfg.Create()
if err != nil {
log.Fatal(err)
}
err = pdfg.WriteFile("./sh.pdf")
if err != nil {
log.Fatal(err)
}
photoBytes, err := ioutil.ReadFile("./sh.pdf")
if err != nil {
panic(err)
}
fname := fmt.Sprintf("Расписание %d неделя.pdf", week-bot.Week)
photoFileBytes := tgbotapi.FileBytes{
Name: fname,
Bytes: photoBytes,
}
msg := tgbotapi.NewDocument(bot.TG_user.TgId, photoFileBytes)
_, err = bot.TG.Send(msg)
if err != nil {
log.Println(err)
}
var shId int64
if isPersonal {
shId = 0
} else {
shId = shedules[0].SheduleId
}
markup := SummaryKeyboard(
"week",
shId,
shedules[0].IsTeacher,
dw,
)
str := fmt.Sprintf("Расписание на %d неделю сообщением ниже 👇", week-bot.Week)
bot.EditOrSend(str, markup, editMsg...)
}
const head = `<html lang="ru">
<head>
<meta charset="UTF-8">
<title>Тестовая страница с расписанием</title>
<link rel="stylesheet" href="./rasp.css\">
<meta name='viewport' content='width=device-width,initial-scale=1'/>
<meta name="mobile-web-app-capable" content="yes">
</head>
<style>
.note div,.rasp div{background-color:#f0f8ff;padding:10px;text-align:center;border-radius:10px}.note,th.head,th.time{font-family:monospace}.subj div #text,.subj p{display:none}html{font-size:1.3rem}body{background:#dc14bd}table{table-layout:fixed;width:100%;border-spacing:5px 5px}.note div{margin:10px 0}.head p,.subj p,hr{margin:0}.rasp div{transition:.3s}th.head{background-color:#0ff;border-radius:10px;padding:5px;font-size:1.05rem}th.subj,th.time{background-color:#f0f8ff;padding:10px;border-radius:10px}th.time{font-size:1.1rem}.subj h2,.subj p{font-size:.85rem}th.subj:not(.lab,.lect,.pract,.other){background-color:#a9a9a9}.subj div{border-radius:10px;padding:5px}.subj p{font-family:monospace;color:#f0f8ff}.subj h2,.subj h3,.subj h5{font-family:monospace;text-align:left;margin:5px}.subj h3{font-size:.65rem}.subj h5{font-size:.7rem;font-weight:400}.lect div{background-color:#7fff00}.pract div{background-color:#dc143c}.lab div{background-color:#8a2be2}.other div{background-color:#ff8c00}
</style>
<body>
`
const lessonHead = `<div class="subj %s">\n'
<div><p></p></div>\n'
<h2>%s</h2><hr>\n`
const lessonHead = `<th class="subj %s" valign="top">
<div><p></p></div>
<h2>%s</h2><hr>`
var weekdays = [6]string{
"пн",
@ -117,26 +183,41 @@ var weekdays = [6]string{
func (bot *Bot) CreateHTMLShedule(week int, shedule [][6][]database.Lesson, dates []time.Time, times []ssau_parser.Lesson) string {
html := head
html += fmt.Sprintf("<div class=\"note\"><div id=\"week\">%d неделя</div></div>\n", week)
html += "<div class=\"rasp\">\n<div class=\"head\">Время</div>\n"
html += fmt.Sprintf("<div class=\"note\"><div id=\"week\">%d неделя</div></div>\n", week-bot.Week)
html += "<table class=\"rasp\">\n<tr><th class=\"head\" style=\"width: 4rem\">Время</th>\n"
for i, d := range dates {
day := d.Format("02")
html += fmt.Sprintf("<div class=\"head\">%s<p>%s</p></div>", weekdays[i], day)
html += fmt.Sprintf("<th class=\"head\">%s<p>%s</p></th>", weekdays[i], day)
}
html += "</tr>\n"
for t, tline := range shedule {
begin := times[t].Begin.Format("15:04")
end := times[t].End.Format("15:04")
html += fmt.Sprintf("<div class=\"time\">%s<hr>%s</div>", begin, end)
for _, l := range tline {
html += fmt.Sprintf("<tr>\n<th class=\"time\">%s<hr>%s</th>", begin, end)
for i, l := range tline {
if len(l) > 0 {
html += fmt.Sprintf(lessonHead, l[0].Type, l[0].Name)
html += "</div>\n"
if l[0].TeacherId != 0 {
var t database.Teacher
bot.DB.ID(l[0].TeacherId).Get(&t)
name := GenerateName(t)
html += fmt.Sprintf("<h5 id=\"prep\">%s</h5>\n", name)
}
if l[0].Place != "" {
html += fmt.Sprintf("<h3>%s</h3>\n", l[0].Place)
}
html += "</th>\n"
} else {
html += "<div class=\"subj\"></div>\n"
html += "<th class=\"subj\"></th>\n"
}
if i%7 == 6 {
html += "</tr>\n"
}
}
}
html += "</div></body></html>"
html += "</table></body></html>"
return html
}