123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857 |
- package services
- import (
- "baliance.com/gooxml/color"
- "baliance.com/gooxml/document"
- "baliance.com/gooxml/measurement"
- "baliance.com/gooxml/schema/soo/ofc/sharedTypes"
- "baliance.com/gooxml/schema/soo/wml"
- "bytes"
- "encoding/json"
- "errors"
- "fmt"
- wkhtml "github.com/SebastiaanKlippert/go-wkhtmltopdf"
- "github.com/shopspring/decimal"
- "hongze/hz_crm_api/models/contract"
- "hongze/hz_crm_api/models/contract/request"
- "hongze/hz_crm_api/utils"
- "html/template"
- "os"
- "os/exec"
- "path"
- "reflect"
- "strconv"
- "strings"
- "time"
- )
- type TableData struct {
- List []TableRow `json:"table" description:"列数据"`
- }
- type TableRow struct {
- RowList []TableCel `json:"row" description:"列数据"`
- }
- type TableCel struct {
- Value string `json:"value" description:"展示的数据"`
- ColumnSpan int `json:"column_span" description:"需要合同的列数量"`
- RowSpan int `json:"row_span" description:"需要合同的行数量"`
- IsMerged bool `json:"is_merged" description:"是否需要上下行合并"`
- IsFirstMerged bool `json:"is_first_merged" description:"是否是第一次合并上下行"`
- Background string `json:"background" description:"背景色"`
- IsBold bool `json:"is_bold" description:"是否加粗显示"`
- TextAlign string `json:"text_align" description:"对齐方式"`
- FontSize float64 `json:"font_size" description:"字体大小"`
- WidthPercent float64 `json:"width_percent" description:"单元格宽度占整个表格的百分比"`
- }
- func getColorConf(background string) (foreground color.Color) {
- switch background {
- case "slate_gray":
- foreground = color.SlateGray
- case "light_slate_gray":
- foreground = color.LightSlateGray
- case "light_gray":
- foreground = color.LightGray
- case "gray":
- foreground = color.Gray
- case "gray_1":
- foreground = color.RGB(uint8(215), uint8(215), uint8(215))
- case "gray_2":
- foreground = color.RGB(uint8(241), uint8(241), uint8(241))
- case "dim_gray":
- foreground = color.DimGray
- case "dark_slate_gray":
- foreground = color.DarkSlateGray
- default:
- foreground = color.LightGray
- }
- return
- }
- func getTextAlignConf(textAlign string) (align wml.ST_Jc) {
- switch textAlign {
- case "left":
- align = wml.ST_JcLeft
- case "center":
- align = wml.ST_JcCenter
- case "right":
- align = wml.ST_JcRight
- case "both":
- align = wml.ST_JcBoth
- default:
- align = wml.ST_JcLeft
- }
- return
- }
- func GenerateWord(contractDetail *contract.ContractDetail) (err error) {
- wordTemplatePath := getWordPath(contractDetail.TemplateId)
- if wordTemplatePath == "" {
- err = errors.New("找不到对应的合同模板")
- return
- }
- doc, err := document.Open(wordTemplatePath)
- if err != nil {
- fmt.Println("error opening document: %s", err)
- return
- }
- paragraphs := []document.Paragraph{}
- for _, p := range doc.Paragraphs() {
- paragraphs = append(paragraphs, p)
- }
-
-
-
- for _, sdt := range doc.StructuredDocumentTags() {
- for _, p := range sdt.Paragraphs() {
- paragraphs = append(paragraphs, p)
- }
- }
- doc.AddParagraph()
- for _, p := range paragraphs {
- for _, r := range p.Runs() {
- switch r.Text() {
- case "{{address}}":
-
-
- r.ClearContent()
- address := getContractAddress(contractDetail)
- r.AddText(address)
-
-
-
-
-
-
-
-
- case "{{postcode}}":
- r.ClearContent()
- r.AddText(contractDetail.Postcode)
- case "{{phone}}":
- r.ClearContent()
- r.AddText(contractDetail.Phone)
- case "{{fax}}":
- r.ClearContent()
- r.AddText(contractDetail.Fax)
- case "{{remark}}":
- r.ClearContent()
- remark := contractDetail.Remark
- if remark == "" {
- remark = "无"
- }
- r.AddText(remark)
- case "{{start_date}}":
- r.ClearContent()
- r.AddText(contractDetail.StartDate.Format("2006 年 01 月 02 日"))
- case "{{end_date}}":
- r.ClearContent()
- r.AddText(contractDetail.EndDate.Format("2006 年 01 月 02 日"))
- case "{{num_year}}":
- r.ClearContent()
-
- newDecimal := decimal.NewFromFloat(contractDetail.EndDate.Sub(contractDetail.StartDate).Hours())
-
- newDecimal2 := decimal.NewFromInt(24 * 365)
-
- numYearDecimal := newDecimal.Div(newDecimal2).Round(1)
-
- minDecimal := decimal.NewFromFloat(0.1)
-
- if numYearDecimal.LessThan(minDecimal) {
- numYearDecimal = minDecimal
- }
-
-
-
-
-
- r.AddText(numYearDecimal.String())
- case "{{price}}":
- r.ClearContent()
- priceStr := ""
-
-
-
- originalPrice := utils.FormatPrice(contractDetail.OriginalPrice)
- priceStr += "小写:" + originalPrice + ","
-
- originalCnyPrice, cnyErr := utils.ConvertNumToCny(contractDetail.OriginalPrice)
- if cnyErr != nil {
- err = cnyErr
- return
- }
- priceStr += "大写:" + originalCnyPrice
-
- if contractDetail.OriginalPrice != contractDetail.Price {
-
-
- price := utils.FormatPrice(contractDetail.Price)
- priceStr += ",经甲乙双方友好协商,优惠至:" + price + "元,"
-
- cnyPrice, cnyErr := utils.ConvertNumToCny(contractDetail.Price)
- if cnyErr != nil {
- err = cnyErr
- return
- }
- priceStr += "大写:" + cnyPrice
- }
- r.AddText(priceStr)
- case "{{pay_remark}}":
- r.ClearContent()
- r.AddText(contractDetail.PayRemark)
- case "{{company_name}}":
- r.ClearContent()
- r.AddText(contractDetail.CompanyName)
-
- case "{{services}}":
- r.ClearContent()
-
- nowParagraph := p
- for i := len(contractDetail.Service) - 1; i >= 0; i-- {
-
- var tableDataList TableData
-
- tableTitle := ""
- item := contractDetail.Service[i]
-
- if item.HasDetail == "是" && len(item.DetailList) > 0 {
-
- tableRowList := make([]TableRow, 0)
-
- for j := 0; j < len(item.DetailList); j++ {
-
- isBold := false
- backgrandColor := ""
- fontSize := 10.0
-
- if j == 0 {
- isBold = true
- backgrandColor = "gray_2"
- fontSize = 12.0
- }
-
- tmpCellList, colErr := getColList(item.DetailList[j])
- if colErr != nil {
- err = colErr
- return
- }
-
- tableCelList := make([]TableCel, 0)
- lenCell := len(tmpCellList)
- for k := 0; k < len(tmpCellList); k++ {
-
-
- newDecimal := decimal.NewFromFloat(100)
-
- newDecimal2 := decimal.NewFromInt(int64(lenCell))
-
- widthPercent, _ := newDecimal.Div(newDecimal2).Round(3).Float64()
-
-
-
-
-
- tableCel := TableCel{
- Value: tmpCellList[k],
- TextAlign: "center",
-
-
- Background: backgrandColor,
- IsBold: isBold,
- FontSize: fontSize,
- WidthPercent: widthPercent,
- }
- tableCelList = append(tableCelList, tableCel)
- }
-
- tableRow := TableRow{
- RowList: tableCelList,
- }
- tableRowList = append(tableRowList, tableRow)
- }
-
- tableDataList.List = tableRowList
- tableTitle = "依照《弘则研究FICC客户服务列表2022》中 小套餐 的服务内容,详细如下:"
- } else {
-
- contractServiceTemplate, tmpErr := contract.GetContractServiceTemplateById(item.ServiceTemplateId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
-
- jsonStr := contractServiceTemplate.TableValue
- err = json.Unmarshal([]byte(jsonStr), &tableDataList)
- if err != nil {
- return
- }
-
- tableTitle = contractServiceTemplate.Remark
- }
-
- tmpParagraph, tmpErr := addTable(tableTitle, tableDataList, doc, nowParagraph)
- if tmpErr != nil {
- err = tmpErr
- return
- }
-
-
-
- nowParagraph = tmpParagraph
- }
- default:
-
- }
- }
- }
- doc.SaveToFile(fmt.Sprint("./static/word/系统生成合同", contractDetail.ContractId, ".docx"))
- return
- }
- func addTable(title string, tableDataList TableData, doc *document.Document, paragraph document.Paragraph) (nowParagraph document.Paragraph, err error) {
-
-
- nowParagraph = doc.InsertParagraphBefore(paragraph)
- nowRun := nowParagraph.AddRun()
- nowRun.AddBreak()
-
-
-
-
-
-
-
-
-
-
- {
- table := doc.InsertTableAfter(nowParagraph)
-
- table.Properties().SetWidth(6.5 * measurement.Inch)
-
-
-
- borders := table.Properties().Borders()
-
- borders.SetAll(wml.ST_BorderSingle, color.Auto, measurement.Zero)
-
- rowList := tableDataList.List
-
- rowIsMeged := make(map[int]bool)
-
- for i := 0; i < len(rowList); i++ {
-
- row := table.AddRow()
-
- row.Properties().SetHeight(30*measurement.Point, wml.ST_HeightRuleAtLeast)
-
- rowDataList := rowList[i].RowList
- if rowDataList != nil {
- for j := 0; j < len(rowDataList); j++ {
-
- var isMeged bool
- isMeged, ok := rowIsMeged[j]
- if !ok {
- rowIsMeged[j] = false
- isMeged = false
- }
- cell := row.AddCell()
- cellPara := cell.AddParagraph()
- run := cellPara.AddRun()
-
- cellData := rowDataList[j]
-
- if cellData.ColumnSpan > 0 {
-
- cell.Properties().SetColumnSpan(cellData.ColumnSpan)
-
- }
-
- if cellData.IsMerged {
-
- rowIsMeged[j] = true
-
- var mergeVal wml.ST_Merge
- if isMeged {
- mergeVal = wml.ST_MergeContinue
- } else {
- mergeVal = wml.ST_MergeRestart
- }
- cell.Properties().SetVerticalMerge(mergeVal)
- } else {
-
- rowIsMeged[j] = false
- }
-
- if cellData.Background != "" {
- cell.Properties().SetShading(wml.ST_ShdSolid, getColorConf(cellData.Background), color.Auto)
- }
-
- cell.Properties().SetVerticalAlignment(wml.ST_VerticalJcCenter)
-
- if cellData.WidthPercent > 0 {
- cell.Properties().SetWidthPercent(cellData.WidthPercent)
- }
-
- if cellData.TextAlign != "" {
- cellPara.Properties().SetAlignment(getTextAlignConf(cellData.TextAlign))
-
- }
-
-
- run.Properties().SetBold(cellData.IsBold)
-
- fontSize := 10.0
- if cellData.FontSize > 0 {
- fontSize = cellData.FontSize
- }
- run.Properties().SetSize(measurement.Distance(fontSize * measurement.Point))
-
- cellPara.Properties().Spacing().SetLineSpacing(measurement.Distance(1.4*fontSize*measurement.Point), wml.ST_LineSpacingRuleAuto)
-
- cellPara.Properties().Spacing().SetBefore(measurement.Distance(0.9 * fontSize * measurement.Point))
-
- cellPara.Properties().Spacing().SetAfter(measurement.Distance(0.5 * fontSize * measurement.Point))
-
- run.Properties().SetFontFamily("宋体")
-
- run.Properties().SetVerticalAlignment(sharedTypes.ST_VerticalAlignRunBaseline)
-
- if cellData.Value != "" {
- strSlice := strings.Split(cellData.Value, "<br/>")
- for s := 0; s < len(strSlice); s++ {
- if s > 0 {
- run.AddBreak()
- }
- run.AddText(strSlice[s])
- }
- } else {
- run.AddText("")
- }
- }
- }
- }
- }
- return
- }
- func getWordPath(templateId int) string {
- var path string
- switch templateId {
- case 1:
- path = "./static/word/template_1.docx"
- case 2:
- path = "./static/word/template_2.docx"
- }
- return path
- }
- type html2pdfData struct {
- CompanyName string `description:"甲方名称"`
- ContractCode string `description:"合同编号"`
- Address string `description:"甲方地址"`
- PostcodeDisplay string `description:"甲方邮编;是否展示"`
- Postcode string `description:"甲方邮编"`
- PhoneDisplay string `description:"甲方电话;是否展示"`
- Phone string `description:"甲方电话"`
- FaxDisplay string `description:"传真;是否展示"`
- Fax string `description:"传真"`
- RemarkDisplay string `description:"备注;是否展示"`
- Remark string `description:"备注"`
- PayRemark string `description:"支付备注"`
- StartDate string `description:"合同开始日期"`
- EndDate string `description:"合同结束日期"`
- NumYear string `description:"合同有效期"`
- Price string `description:"支付金额"`
- TableHtml string `description:"表格数据"`
- }
- func GetHtmlByContractDetail(contractDetail *contract.ContractDetail, htmlType string) (contractHtml string, err error) {
- tmpContractDetail := *contractDetail
- contractTemplate, err := contract.GetContractTemplateByTemplateId(tmpContractDetail.TemplateId)
- if err != nil {
- return
- }
-
- contentConfigMap := make(map[string]string)
- err = json.Unmarshal([]byte(contractTemplate.ContentConfig), &contentConfigMap)
- if err != nil {
- return
- }
- htmlTpl := contractTemplate.Html
- if htmlType == "pdf" {
- htmlTpl = contractTemplate.PdfHtml
- }
- myTpl := template.Must(template.New("contract").Parse(htmlTpl))
-
- contractServiceAndDetailList := make([]*contract.ContractServiceAndDetail, 0)
- if tmpContractDetail.ContractBusinessType == "代付合同" {
- if tmpContractDetail.RelationContractDetailList != nil && len(tmpContractDetail.RelationContractDetailList) > 0 {
- tmpContractDetail.StartDate = tmpContractDetail.RelationContractDetailList[0].StartDate
- tmpContractDetail.EndDate = tmpContractDetail.RelationContractDetailList[0].EndDate
- contractServiceAndDetailList = tmpContractDetail.RelationContractDetailList[0].Service
- }
- } else {
- contractServiceAndDetailList = tmpContractDetail.Service
- }
-
- address := getContractAddress(&tmpContractDetail)
- data := html2pdfData{
- CompanyName: tmpContractDetail.CompanyName,
- ContractCode: tmpContractDetail.ContractCode,
- Address: address,
- PostcodeDisplay: "block",
- Postcode: tmpContractDetail.Postcode,
- PhoneDisplay: "block",
- Phone: tmpContractDetail.Phone,
- FaxDisplay: "block",
- Fax: tmpContractDetail.Fax,
- RemarkDisplay: "block",
- Remark: tmpContractDetail.Remark,
- PayRemark: tmpContractDetail.PayRemark,
- StartDate: tmpContractDetail.StartDate.Format("2006年01月02日"),
- EndDate: tmpContractDetail.EndDate.Format("2006年01月02日"),
- }
- if data.Postcode == "" {
- data.Postcode = "无"
- data.PostcodeDisplay = "none"
- }
- if data.Fax == "" {
- data.Fax = "无"
- data.FaxDisplay = "none"
- }
- if data.Phone == "" {
- data.Phone = "无"
- data.PhoneDisplay = "none"
- }
- if data.PayRemark == "" {
- data.PayRemark = "无"
- }
- if data.Remark == "" {
- data.Remark = "无"
- data.RemarkDisplay = "none"
- }
-
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- tmpPrintContent, tmpErr := utils.CalculationDate(tmpContractDetail.StartDate, tmpContractDetail.EndDate)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- data.NumYear = tmpPrintContent
- }
-
- {
- priceStr := ""
-
-
-
- originalPrice := utils.FormatPrice(tmpContractDetail.OriginalPrice)
- priceStr += "小写:" + originalPrice + "元,"
-
- originalCnyPrice, cnyErr := utils.ConvertNumToCny(tmpContractDetail.OriginalPrice)
- if cnyErr != nil {
- err = cnyErr
- return
- }
- priceStr += "大写:" + originalCnyPrice
-
- if tmpContractDetail.OriginalPrice != tmpContractDetail.Price {
-
-
- price := utils.FormatPrice(tmpContractDetail.Price)
- priceStr += ",经甲乙双方友好协商,优惠至:" + price + "元,"
-
- cnyPrice, cnyErr := utils.ConvertNumToCny(tmpContractDetail.Price)
- if cnyErr != nil {
- err = cnyErr
- return
- }
- priceStr += "大写:" + cnyPrice
- }
- data.Price = priceStr
- }
- buf := new(bytes.Buffer)
- tplErr := myTpl.Execute(buf, data)
- if tplErr != nil {
- err = tplErr
- return
- }
- contractHtml = buf.String()
-
- {
- tableStr := ""
- tableDataSlice := make([]TableData, 0)
- tableTitleSlice := make([]string, 0)
- title := ``
- if tmpTitle, ok := contentConfigMap["title1"]; ok {
- title = tmpTitle
- }
- for i := 0; i < len(contractServiceAndDetailList); i++ {
-
- var tableDataList TableData
- item := contractServiceAndDetailList[i]
-
- tableTitleSlice = append(tableTitleSlice, item.Title)
-
- if item.HasDetail == "是" && len(item.DetailList) > 0 {
-
- tableRowList := make([]TableRow, 0)
-
- for j := 0; j < len(item.DetailList); j++ {
-
- isBold := false
- backgrandColor := ""
- fontSize := 13.0
-
- if j == 0 {
- isBold = true
- backgrandColor = "gray_2"
- fontSize = 13.0
- }
-
- tmpCellList, colErr := getColList(item.DetailList[j])
- if colErr != nil {
- err = colErr
- return
- }
-
- tableCelList := make([]TableCel, 0)
- lenCell := len(tmpCellList)
- for k := 0; k < len(tmpCellList); k++ {
-
- widthPercent := 30.0
- if k > 0 {
-
-
- newDecimal := decimal.NewFromFloat(70)
-
- newDecimal2 := decimal.NewFromInt(int64(lenCell) - 1)
-
- tmpWidthPercent, _ := newDecimal.Div(newDecimal2).Round(3).Float64()
-
-
-
-
- widthPercent = tmpWidthPercent
-
- }
- tableCel := TableCel{
- Value: tmpCellList[k],
- TextAlign: "center",
-
-
- Background: backgrandColor,
- IsBold: isBold,
- FontSize: fontSize,
- WidthPercent: widthPercent,
- }
- tableCelList = append(tableCelList, tableCel)
- }
-
- tableRow := TableRow{
- RowList: tableCelList,
- }
- tableRowList = append(tableRowList, tableRow)
- }
-
- tableDataList.List = tableRowList
- } else {
-
- jsonStr := item.TableValue
- tmpEerr := json.Unmarshal([]byte(jsonStr), &tableDataList)
- if tmpEerr != nil {
- err = tmpEerr
- return
- }
- }
- tableDataSlice = append(tableDataSlice, tableDataList)
- }
- titleStr := strings.Join(tableTitleSlice, "、")
- title += titleStr + "的服务内容,详细如下:"
- if htmlType == "pdf" {
- tableStr += `<p style="">` + title + `</p>`
- } else {
- tableStr = `<p style="font-size: 13pt; line-height: 40px">` + title + `</p>`
- }
- for _, tableDataList := range tableDataSlice {
-
- if htmlType == "pdf" {
- tableStr += getTableStrByPdf(tableDataList)
- } else {
- tableStr += getTableStr(tableDataList)
- }
- }
- data.TableHtml = tableStr
- }
-
- contractHtml = strings.Replace(contractHtml, `\{\{\{TableHtml\}\}\}`, data.TableHtml, -1)
- return
-
-
-
-
-
-
-
-
-
-
-
-
- }
- func getTableStr(tableDataList TableData) (tableStr string) {
-
- tableStr += `<table style="width: 100%;border-collapse: collapse;font-size: 13pt;margin-bottom:30px;page-break-inside: avoid !important;"><tbody>`
- rowList := tableDataList.List
- for i := 0; i < len(rowList); i++ {
-
- tableStr += `<tr style="`
- tableStr += `page-break-before: always;page-break-after: always;page-break-inside: avoid !important;`
-
- tableStr += `">`
-
-
-
-
-
-
- rowDataList := rowList[i].RowList
- cellStr := ""
- if rowDataList != nil {
- for j := 0; j < len(rowDataList); j++ {
-
-
-
-
- tdStr := `<td `
-
- styleStr := `style="`
- styleStr += `border:1px solid #808181;padding: 15px 10px;line-height: 1.5;`
-
- cellOtherStr := ` valign="middle" `
-
- cellData := rowDataList[j]
-
- if cellData.ColumnSpan > 0 {
-
- cellOtherStr += ` colspan="` + strconv.Itoa(cellData.ColumnSpan) + `" `
- }
-
- if cellData.IsMerged {
- if cellData.IsFirstMerged {
- cellOtherStr += ` rowspan="` + strconv.Itoa(cellData.RowSpan) + `" `
- } else {
-
- continue
- }
- }
-
- if cellData.Background != "" {
- styleStr += `background-color: #F0F2F5;`
- }
-
- if cellData.WidthPercent > 0 {
- widthDecimal := decimal.NewFromFloat(cellData.WidthPercent)
- cellOtherStr += ` width="` + widthDecimal.String() + `%" `
- }
-
- if cellData.TextAlign != "" {
- cellOtherStr += ` align="` + cellData.TextAlign + `" `
- }
-
-
- if cellData.IsBold {
- styleStr += `font-weight:bold;`
- }
-
- fontSize := 10.0
- if cellData.FontSize > 0 {
- fontSize = cellData.FontSize
- }
- fontDecimal := decimal.NewFromFloat(fontSize)
- styleStr += `font-size: ` + fontDecimal.String() + `pt;`
- bodyStr := cellData.Value
- styleStr += `" `
- cellStr += tdStr + styleStr + cellOtherStr + `>` + bodyStr + `</td>`
- }
- }
- tableStr += cellStr + `</tr>`
- }
- tableStr += `</tbody></table>`
- return
- }
- func getTableStrByPdf(tableDataList TableData) (tableStr string) {
-
- tableStr += `<table style="width: 100%;border-collapse: collapse;margin-top:10pt;page-break-inside: avoid !important;"><tbody>`
- rowList := tableDataList.List
- for i := 0; i < len(rowList); i++ {
-
- tableStr += `<tr style="`
- tableStr += `page-break-before: always;page-break-after: always;page-break-inside: avoid !important;`
-
- tableStr += `">`
-
-
-
-
-
-
- rowDataList := rowList[i].RowList
- cellStr := ""
- if rowDataList != nil {
- for j := 0; j < len(rowDataList); j++ {
-
-
-
-
- tdStr := `<td `
-
- styleStr := `style="`
- styleStr += `border:1px solid #808181;padding:4pt 10pt;`
-
- cellOtherStr := ` valign="middle" `
-
- cellData := rowDataList[j]
-
- if cellData.ColumnSpan > 0 {
-
- cellOtherStr += ` colspan="` + strconv.Itoa(cellData.ColumnSpan) + `" `
- }
-
- if cellData.IsMerged {
- if cellData.IsFirstMerged {
- cellOtherStr += ` rowspan="` + strconv.Itoa(cellData.RowSpan) + `" `
- } else {
-
- continue
- }
- }
-
- if cellData.Background != "" {
- styleStr += `background-color: #F0F2F5;`
- }
-
- if cellData.WidthPercent > 0 {
- widthDecimal := decimal.NewFromFloat(cellData.WidthPercent)
- cellOtherStr += ` width="` + widthDecimal.String() + `%" `
- }
-
- if cellData.TextAlign != "" {
- cellOtherStr += ` align="` + cellData.TextAlign + `" `
- }
-
-
- if cellData.IsBold {
- styleStr += `font-weight:bold;`
- }
-
- fontSize := 10.0
- if cellData.FontSize > 0 {
- fontSize = cellData.FontSize
- }
- fontDecimal := decimal.NewFromFloat(fontSize)
- styleStr += `font-size: ` + fontDecimal.String() + `pt;`
- bodyStr := cellData.Value
- styleStr += `" `
- cellStr += tdStr + styleStr + cellOtherStr + `>` + bodyStr + `</td>`
- }
- }
- tableStr += cellStr + `</tr>`
- }
- tableStr += `</tbody></table>`
- return
- }
- func Html2Pdf(htmlStr, pdfPath string) (err error) {
- pdfg, err := wkhtml.NewPDFGenerator()
- if err != nil {
- fmt.Println("err:", err)
- return
- }
-
- page := wkhtml.NewPageReader(strings.NewReader(htmlStr))
-
-
-
-
-
-
-
-
- page.FooterFontSize.Set(8)
- page.FooterRight.Set("[page]")
- page.FooterSpacing.Set(4)
-
-
-
-
-
-
-
- page.EnableTocBackLinks.Set(true)
-
- pdfg.AddPage(page)
-
-
-
-
-
- err = pdfg.Create()
- if err != nil {
- return
- }
- err = pdfg.WriteFile(pdfPath)
- return
- }
- func getColList(item *contract.ContractServiceDetail) (cellList []string, err error) {
- cellList = make([]string, 0)
- var serviceDetailReq request.AddContractServiceDetailReq
- tmpItem := *item
- t := reflect.TypeOf(tmpItem)
- v := reflect.ValueOf(tmpItem)
- for k := 0; k < t.NumField(); k++ {
-
- tmpName := t.Field(k).Name
- if strings.Contains(tmpName, "Col") {
-
- tmpValue := v.Field(k).String()
-
- if tmpValue != "" {
- err = json.Unmarshal([]byte(tmpValue), &serviceDetailReq)
- if err != nil {
- return
- } else {
- cellList = append(cellList, serviceDetailReq.Value)
- }
- }
- }
- }
- return
- }
- type WordElement struct {
- ElementType string `json:"element_type" description:"元素类型"`
- ElementName string `json:"element_name" description:"元素名称"`
- RelationName string `json:"relation_name" description:"关联元素名称"`
- Content string `json:"content" description:"元素内容"`
- Background string `json:"background" description:"背景色"`
- IsBold bool `json:"is_bold" description:"是否加粗显示"`
- TextAlign string `json:"text_align" description:"对齐方式"`
- FontSize float64 `json:"font_size" description:"字体大小"`
- ElementList []WordElement `json:"list" description:"子元素"`
- }
- func GenerateWordV2(contractDetail *contract.ContractDetail, wordPath string) (err error) {
-
- tmpContractDetail := *contractDetail
-
- contractServiceAndDetailList := make([]*contract.ContractServiceAndDetail, 0)
- if tmpContractDetail.ContractBusinessType == "代付合同" {
- if contractDetail.RelationContractDetailList != nil && len(contractDetail.RelationContractDetailList) > 0 {
- tmpContractDetail.StartDate = tmpContractDetail.RelationContractDetailList[0].StartDate
- tmpContractDetail.EndDate = tmpContractDetail.RelationContractDetailList[0].EndDate
- contractServiceAndDetailList = tmpContractDetail.RelationContractDetailList[0].Service
- }
- } else {
- contractServiceAndDetailList = tmpContractDetail.Service
- }
- contractTemplate, err := contract.GetContractTemplateByTemplateId(tmpContractDetail.TemplateId)
- if err != nil {
- return
- }
-
- contentConfigMap := make(map[string]string)
- err = json.Unmarshal([]byte(contractTemplate.ContentConfig), &contentConfigMap)
- if err != nil {
- return
- }
-
- jsonStr := contractTemplate.WordConfig
- var contractData []WordElement
- err = json.Unmarshal([]byte(jsonStr), &contractData)
- if err != nil {
- fmt.Println("json字符串解析失败,ERR:", err)
- return
- }
- doc := document.New()
-
- cp := doc.CoreProperties
-
- cp.SetTitle("弘则弥道(上海)投资咨询有限公司 & 研究服务合同")
- cp.SetAuthor("弘则弥道(上海)投资咨询有限公司")
- cp.SetCategory("合同")
-
- cp.SetLastModifiedBy("弘则弥道(上海)投资咨询有限公司")
- cp.SetCreated(time.Now())
- cp.SetModified(time.Now())
- cp.SetDescription("弘则弥道(上海)投资咨询有限公司 研究服务合同")
- cp.SetLanguage("中文")
- tableFontSize := 10.5
- for _, data := range contractData {
- fontSize := data.FontSize
- if fontSize <= 0 {
- fontSize = 15
- }
- printContent := ``
- if data.ElementName == "services" {
- tableTitleSlice := make([]string, 0)
- title := ``
- if tmpTitle, ok := contentConfigMap["title1"]; ok {
- title = tmpTitle
- }
- TableDataListSlice := make([]TableData, 0)
-
- for i := 0; i < len(contractServiceAndDetailList); i++ {
-
- var tableDataList TableData
- item := contractServiceAndDetailList[i]
-
- tableTitleSlice = append(tableTitleSlice, item.Title)
-
- if item.HasDetail == "是" && len(item.DetailList) > 0 {
-
- tableRowList := make([]TableRow, 0)
-
- for j := 0; j < len(item.DetailList); j++ {
-
- isBold := false
- backgrandColor := ""
- fontSize := tableFontSize
-
- if j == 0 {
- isBold = true
- backgrandColor = "gray_2"
- }
-
- tmpCellList, colErr := getColList(item.DetailList[j])
- if colErr != nil {
- err = colErr
- return
- }
-
- tableCelList := make([]TableCel, 0)
- lenCell := len(tmpCellList)
- for k := 0; k < len(tmpCellList); k++ {
-
-
- newDecimal := decimal.NewFromFloat(100)
-
- newDecimal2 := decimal.NewFromInt(int64(lenCell))
-
- widthPercent, _ := newDecimal.Div(newDecimal2).Round(3).Float64()
-
-
-
-
-
- tableCel := TableCel{
- Value: tmpCellList[k],
- TextAlign: "center",
-
-
- Background: backgrandColor,
- IsBold: isBold,
- FontSize: fontSize,
- WidthPercent: widthPercent,
- }
- tableCelList = append(tableCelList, tableCel)
- }
-
- tableRow := TableRow{
- RowList: tableCelList,
- }
- tableRowList = append(tableRowList, tableRow)
- }
-
- tableDataList.List = tableRowList
- } else {
-
- jsonStr := item.TableValue
- err = json.Unmarshal([]byte(jsonStr), &tableDataList)
- if err != nil {
- return
- }
- }
-
- TableDataListSlice = append(TableDataListSlice, tableDataList)
- }
-
- titleStr := strings.Join(tableTitleSlice, "、")
- title += titleStr + "的服务内容,详细如下:"
-
- headerPar := doc.AddParagraph()
- headerParPro := headerPar.Properties()
-
- textAlign := getTextAlignConf(data.TextAlign)
- headerParPro.SetAlignment(textAlign)
-
-
-
- headerRun := headerPar.AddRun()
- headerRunPro := headerRun.Properties()
- headerRunPro.SetBold(data.IsBold)
- headerRunPro.SetSize(measurement.Distance(fontSize * measurement.Point))
- headerRunPro.SetFontFamily("宋体")
-
- headerRun.AddText(title)
-
- for _, tableDataList := range TableDataListSlice {
- tmpErr := addTableV2(tableDataList, doc)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- }
- continue
- } else {
- isPrint, tmpPrintContent, tmpErr := getPrintData(data, &tmpContractDetail)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- if isPrint == false {
- continue
- }
- printContent = tmpPrintContent
- }
-
-
- printContentRune := []rune(printContent)
- strLen := len(printContentRune)
-
-
- addTabNum := 0
- printContentList := make([]map[int]string, 0)
- firstLen := 17
- secondLen := 14
- if data.ElementType == "column" {
- if strLen > firstLen {
- maxLine := ((strLen - firstLen) / secondLen) + 1
- for i := 0; i < maxLine; i++ {
- printContentMap := make(map[int]string)
- startIndex := secondLen*i + firstLen
- endIndex := secondLen*(i+1) + firstLen
- if endIndex > strLen {
- endIndex = strLen
- }
- tmpPrintContent := string(printContentRune[startIndex:endIndex])
- printContentMap[0] = tmpPrintContent
- printContentList = append(printContentList, printContentMap)
- }
- printContent = string(printContentRune[:firstLen])
- strLen = firstLen
-
-
-
- } else {
- addTabNum = firstLen - strLen
- }
- addTabNum += 3
- }
-
- headerPar := doc.AddParagraph()
- headerParPro := headerPar.Properties()
- headerParPro.Spacing().SetLineSpacing(measurement.Distance(1.5*fontSize*measurement.Point), wml.ST_LineSpacingRuleAuto)
-
- textAlign := getTextAlignConf(data.TextAlign)
- headerParPro.SetAlignment(textAlign)
-
-
-
- headerRun := headerPar.AddRun()
- headerRunPro := headerRun.Properties()
- headerRunPro.SetBold(data.IsBold)
- headerRunPro.SetSize(measurement.Distance(fontSize * measurement.Point))
- headerRunPro.SetFontFamily("宋体")
-
- headerRun.AddText(printContent)
- for _, text := range data.ElementList {
- if text.ElementName == "services" {
- tableTitleSlice := make([]string, 0)
- title := ``
- if tmpTitle, ok := contentConfigMap["title1"]; ok {
- title = tmpTitle
- }
- TableDataListSlice := make([]TableData, 0)
- for i := len(contractServiceAndDetailList) - 1; i >= 0; i-- {
-
- var tableDataList TableData
- item := contractServiceAndDetailList[i]
-
- tableTitleSlice = append(tableTitleSlice, item.Title)
-
- if item.HasDetail == "是" && len(item.DetailList) > 0 {
-
- tableRowList := make([]TableRow, 0)
-
- for j := 0; j < len(item.DetailList); j++ {
-
- isBold := false
- backgrandColor := ""
- fontSize := tableFontSize
-
- if j == 0 {
- isBold = true
- backgrandColor = "gray_2"
- }
-
- tmpCellList, colErr := getColList(item.DetailList[j])
- if colErr != nil {
- err = colErr
- return
- }
-
- tableCelList := make([]TableCel, 0)
- lenCell := len(tmpCellList)
- for k := 0; k < len(tmpCellList); k++ {
-
-
- newDecimal := decimal.NewFromFloat(100)
-
- newDecimal2 := decimal.NewFromInt(int64(lenCell))
-
- widthPercent, _ := newDecimal.Div(newDecimal2).Round(3).Float64()
-
-
-
-
-
- tableCel := TableCel{
- Value: tmpCellList[k],
- TextAlign: "center",
-
-
- Background: backgrandColor,
- IsBold: isBold,
- FontSize: fontSize,
- WidthPercent: widthPercent,
- }
- tableCelList = append(tableCelList, tableCel)
- }
-
- tableRow := TableRow{
- RowList: tableCelList,
- }
- tableRowList = append(tableRowList, tableRow)
- }
-
- tableDataList.List = tableRowList
- } else {
-
- jsonStr := item.TableValue
- err = json.Unmarshal([]byte(jsonStr), &tableDataList)
- if err != nil {
- return
- }
- }
-
- TableDataListSlice = append(TableDataListSlice, tableDataList)
- }
-
- titleStr := strings.Join(tableTitleSlice, "、")
- title += titleStr + "的服务内容,详细如下:"
- headerRun := headerPar.AddRun()
- headerRun.AddBreak()
- headerRunPro := headerRun.Properties()
- headerRunPro.SetBold(text.IsBold)
- headerRunPro.SetSize(measurement.Distance(fontSize * measurement.Point))
- headerRunPro.SetFontFamily("宋体")
-
- headerRun.AddText(title)
-
- for _, tableDataList := range TableDataListSlice {
- tmpErr := addTableV2(tableDataList, doc)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- }
- } else {
- isPrint, printContent, tmpErr := getPrintData(text, &tmpContractDetail)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- if isPrint == false {
- continue
- }
- if data.ElementType == "column" {
- for j := 0; j < addTabNum; j++ {
- tabStr := " "
- printContent = tabStr + printContent
- }
- }
- fontSize := text.FontSize
- if fontSize <= 0 {
- fontSize = 15
- }
- headerRun2 := headerPar.AddRun()
- headerRunPro2 := headerRun2.Properties()
- headerRunPro2.SetBold(text.IsBold)
- headerRunPro2.SetSize(measurement.Distance(fontSize * measurement.Point))
- headerRunPro2.SetFontFamily("宋体")
-
- headerRun2.AddText(printContent)
- }
- }
- for _, printMap := range printContentList {
-
- headerPar := doc.AddParagraph()
- headerParPro := headerPar.Properties()
- headerParPro.Spacing().SetLineSpacing(measurement.Distance(1.5*fontSize*measurement.Point), wml.ST_LineSpacingRuleAuto)
-
- textAlign := getTextAlignConf(data.TextAlign)
- headerParPro.SetAlignment(textAlign)
-
- headerParPro.SetStartIndent(measurement.Distance(3 * fontSize * measurement.Point))
-
-
-
-
-
-
- headerRun := headerPar.AddRun()
- headerRunPro := headerRun.Properties()
- headerRunPro.SetBold(data.IsBold)
- headerRunPro.SetSize(measurement.Distance(fontSize * measurement.Point))
- headerRunPro.SetFontFamily("宋体")
-
- headerRun.AddText(printMap[0])
- }
- }
-
-
-
-
-
-
-
-
-
-
- err = doc.SaveToFile(wordPath)
- return
- }
- func getPrintData(data WordElement, contractDetail *contract.ContractDetail) (isPrint bool, printContent string, err error) {
- printContent = data.Content
- if data.RelationName != "" {
- switch data.RelationName {
- case "address":
- if contractDetail.Address == "" && contractDetail.Province == "" && contractDetail.City == "" {
- return
- }
- case "postcode":
- if contractDetail.Postcode == "" {
- return
- }
- case "phone":
- if contractDetail.Phone == "" {
- return
- }
- case "fax":
- if contractDetail.Fax == "" {
- return
- }
- case "remark":
- if contractDetail.Remark == "" {
- return
- }
- case "price":
-
- if contractDetail.OriginalPrice == contractDetail.Price {
- return
- }
- case "price_cn":
-
- if contractDetail.OriginalPrice == contractDetail.Price {
- return
- }
- case "pay_remark":
- if contractDetail.PayRemark == "" {
- return
- }
- case "company_name":
- if contractDetail.CompanyName == "" {
- return
- }
- }
- }
- switch data.ElementName {
- case "address":
- if contractDetail.Address == "" && contractDetail.Province == "" && contractDetail.City == "" {
- return
- }
- printContent = getContractAddress(contractDetail)
- case "postcode":
- if contractDetail.Postcode == "" {
- return
- }
- printContent = contractDetail.Postcode
- case "phone":
- if contractDetail.Phone == "" {
- return
- }
- printContent = contractDetail.Phone
- case "fax":
- if contractDetail.Fax == "" {
- return
- }
- printContent = contractDetail.Fax
- case "remark":
- if contractDetail.Remark == "" {
- return
- }
- printContent = contractDetail.Remark
- case "start_date":
- printContent = contractDetail.StartDate.Format("2006年01月02日")
- case "end_date":
- printContent = contractDetail.EndDate.Format("2006年01月02日")
- case "num_year":
-
-
-
-
-
-
-
-
-
-
-
-
-
- tmpPrintContent, tmpErr := utils.CalculationDate(contractDetail.StartDate, contractDetail.EndDate)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- printContent = tmpPrintContent
- case "original_price":
-
-
- printContent = utils.FormatPrice(contractDetail.OriginalPrice)
- case "original_price_cn":
-
- originalCnyPrice, cnyErr := utils.ConvertNumToCny(contractDetail.OriginalPrice)
- if cnyErr != nil {
- err = cnyErr
- return
- }
- printContent = originalCnyPrice
- case "price":
-
- if contractDetail.OriginalPrice == contractDetail.Price {
- return
- }
-
- printContent = utils.FormatPrice(contractDetail.Price)
- case "price_cn":
-
- if contractDetail.OriginalPrice == contractDetail.Price {
- return
- }
- cnyPrice, cnyErr := utils.ConvertNumToCny(contractDetail.Price)
- if cnyErr != nil {
- err = cnyErr
- return
- }
- printContent = cnyPrice
- case "pay_remark":
- if contractDetail.PayRemark == "" {
- return
- }
- printContent = contractDetail.PayRemark
- case "company_name":
- if contractDetail.CompanyName == "" {
- return
- }
- printContent = contractDetail.CompanyName
- case "company_name_sign":
- if contractDetail.CompanyName == "" {
- return
- }
- printContent = "甲方:" + contractDetail.CompanyName
- }
- isPrint = true
- return
- }
- func addTableV2(tableDataList TableData, doc *document.Document) (err error) {
-
-
-
-
-
- doc.AddParagraph().Properties().AddSection(wml.ST_SectionMarkNextPage)
- table := doc.AddTable()
-
- tableWidth := 6.5
- table.Properties().SetWidth(measurement.Distance(tableWidth * measurement.Inch))
-
-
-
- borders := table.Properties().Borders()
-
- borders.SetAll(wml.ST_BorderSingle, color.Auto, measurement.Zero)
-
- rowList := tableDataList.List
-
- for i := 0; i < len(rowList); i++ {
-
- row := table.AddRow()
-
- row.Properties().SetHeight(20*measurement.Point, wml.ST_HeightRuleAtLeast)
-
- rowDataList := rowList[i].RowList
- if rowDataList != nil {
- for j := 0; j < len(rowDataList); j++ {
- cell := row.AddCell()
- cellPara := cell.AddParagraph()
- run := cellPara.AddRun()
-
- cellData := rowDataList[j]
-
- if cellData.ColumnSpan > 0 {
-
- cell.Properties().SetColumnSpan(cellData.ColumnSpan)
-
- }
-
- if cellData.IsMerged {
-
- var mergeVal wml.ST_Merge
- if cellData.IsFirstMerged {
- mergeVal = wml.ST_MergeRestart
- } else {
- mergeVal = wml.ST_MergeContinue
- }
- cell.Properties().SetVerticalMerge(mergeVal)
- }
-
- if cellData.Background != "" {
- cell.Properties().SetShading(wml.ST_ShdSolid, getColorConf(cellData.Background), color.Auto)
- }
-
- cell.Properties().SetVerticalAlignment(wml.ST_VerticalJcCenter)
-
- if cellData.WidthPercent > 0 {
-
-
- cellWidth := tableWidth * cellData.WidthPercent * measurement.Inch / 100
- cell.Properties().SetWidth(measurement.Distance(cellWidth))
- }
-
- if cellData.TextAlign != "" {
- cellPara.Properties().SetAlignment(getTextAlignConf(cellData.TextAlign))
-
- }
-
-
- run.Properties().SetBold(cellData.IsBold)
-
- fontSize := 10.5
- if cellData.FontSize > 0 {
- fontSize = cellData.FontSize
- }
- run.Properties().SetSize(measurement.Distance(fontSize * measurement.Point))
-
- cellPara.Properties().Spacing().SetLineSpacing(measurement.Distance(1.4*fontSize*measurement.Point), wml.ST_LineSpacingRuleAuto)
-
- cellPara.Properties().Spacing().SetBefore(measurement.Distance(0.9 * fontSize * measurement.Point))
-
- cellPara.Properties().Spacing().SetAfter(measurement.Distance(0.5 * fontSize * measurement.Point))
-
- run.Properties().SetFontFamily("宋体")
-
- run.Properties().SetVerticalAlignment(sharedTypes.ST_VerticalAlignRunBaseline)
-
- if cellData.Value != "" {
- strSlice := strings.Split(cellData.Value, "<br/>")
- for s := 0; s < len(strSlice); s++ {
- if s > 0 {
- run.AddBreak()
- }
- run.AddText(strSlice[s])
- }
- } else {
- run.AddText("")
- }
- }
- }
- }
-
-
- doc.AddParagraph()
- return
- }
- func getContractAddress(contractDetail *contract.ContractDetail) (address string) {
- ignoreStrs := []string{"北京市", "上海市", "天津市", "重庆市"}
- if strings.Contains(strings.Join(ignoreStrs, ","), contractDetail.Province) {
- address = contractDetail.City + contractDetail.Address
- } else {
- address = contractDetail.Province + contractDetail.City + contractDetail.Address
- }
- return
- }
- func FuncDocs2Pdf(command string, fileSrcPath string, fileOutDir string, converterType string) (fileOutPath string, error error) {
-
- srcFile, erByOpenSrcFile := os.Open(fileSrcPath)
- if erByOpenSrcFile != nil && os.IsNotExist(erByOpenSrcFile) {
- return "", erByOpenSrcFile
- }
-
- outFileDir, erByOpenFileOutDir := os.Open(fileOutDir)
- if erByOpenFileOutDir != nil && os.IsNotExist(erByOpenFileOutDir) {
- erByCreateFileOutDir := os.MkdirAll(fileOutDir, os.ModePerm)
- if erByCreateFileOutDir != nil {
- fmt.Println("File ouput dir create error.....", erByCreateFileOutDir.Error())
- return "", erByCreateFileOutDir
- }
- }
-
- defer func() {
- _ = srcFile.Close()
- _ = outFileDir.Close()
- }()
-
- cmd := exec.Command(command, "--invisible", "--convert-to", converterType,
- fileSrcPath, "--outdir", fileOutDir)
- _, errByCmdStart := cmd.Output()
-
- if errByCmdStart != nil {
- return "", errByCmdStart
- }
-
- fileOutPath = fileOutDir + "/" + strings.Split(path.Base(fileSrcPath), ".")[0]
- if converterType == "html" {
- fileOutPath += ".html"
- } else {
- fileOutPath += ".pdf"
- }
-
- return fileOutPath, nil
- }
|