Skip to content

Instantly share code, notes, and snippets.

@x86128
Created November 8, 2016 00:46
Show Gist options
  • Save x86128/0e2862ce42eeeeb58c7815e4bfa4a35e to your computer and use it in GitHub Desktop.
Save x86128/0e2862ce42eeeeb58c7815e4bfa4a35e to your computer and use it in GitHub Desktop.
import streams, "source/nimpdf"
# используется библиотека nimpdf
var iFile = open("oper-instr.txt")
var oFile = newFileStream("book.pdf", fmWrite)
var opts = makeDocOpt()
opts.addFontsPath("/usr/share/fonts/truetype/freefont/")
var doc = initPDF(opts)
let pSize = getSizeFromName("A4")
var xPos = 8'f64
var yPos = 15'f64
var yMargin = 15'f64
var yStep = 4.9'f64
doc.addPage(pSize, PGO_PORTRAIT)
doc.setFont("FreeMono", {FS_REGULAR}, 5'f64)
var line = ""
while readLine(iFile, line):
if line[0] == '*':
# ищем окончание управляющей последовательности
var i = 1
while i < len(line)-1 and line[i] != ' ':
inc(i)
# если это текст
if i<len(line):
if line[i-1] == 'D':
# печатаем строку
doc.drawText(xPos, yPos, line[i..len(line)-1])
yPos += yStep
# если это разрыв страницы
if i > 2 and line[i-3..i-1] == "*3/":
yPos = yMargin
# добавляем новую страницу
doc.addPage(pSize, PGO_PORTRAIT)
doc.setFont("FreeMono", {FS_REGULAR}, 5'f64)
# если число строк вышло за пределы страницы
if yPos > 270:
yPos = yMargin
# то начинаем новую
doc.addPage(pSize, PGO_PORTRAIT)
doc.setFont("FreeMono", {FS_REGULAR}, 5'f64)
# пишем PDF
doc.writePDF(oFile)
# и закрываем за собой файлы
close(oFile)
close(iFile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment