@Michele: Thanks for that, I will make good use of it! :)
@Massimo: scaling is one of those options that is more often set by
the pdf viewer (because what generates the pdf usually sets the
default to "scaling=enabled"). If I re-use that fingerboard example:
When using the app, the young violinist, will be asked to measure his/
her violin's scale length (from nut to bridge). then enters the
measurement, exactly. so by simply using the rule of 18 (really
17.817), it generates that template that he/she can print and use to
out his position markers on the violin. The important thing, is that
the printed pdf must be exact, if not he/she will be pitchy. Which
means, if scaling is enabled, the printer will print in a wysisyg
fashion (fits the screen)... which is completely wrong. there must be
no scaling at all, print the exact measutrements.
Since PDF seems to be a hot issue, here's how I use reportlab to
generate the pdf templates (but now, I want to use the web2py builtin
libs) - I know, not too too pretty, but works :)
Thanks,
Mart :)
'''
@author: mart
'''
import os, sys, string
from pyFret.reportlab.pdfgen.canvas import Canvas
from pyFret.reportlab.lib.pagesizes import letter, A4
from pyFret.reportlab.lib.units import cm, mm, inch, pica
from pyFret.reportlab.graphics.charts.textlabels import Label
from pyFret.reportlab.lib import colors
#####################################################
################################################M####
## create string instrument position markers
####A###
##################################################R##
###################################################T#
"""
ref. 4/4 = 32.6 ou 32.7 cm
rect(x, y, width, height, stroke=1, fill=0)
RGB Brown =(107,66,38)
RGB Purple = (135,31,120)
"""
###################################
# rule of 18, unrounded to 17.817 #
###################################
n = 17.817
#########################################
# number of position markers to create #
#########################################
markers = 12
#######################################
# list of standard markers as indices #
#######################################
indexList = [1,3,4,6,8,10,11]
#####################
# string resources #
#####################
sJoin = ' join at previous marker'
def main():
##################################################################################################
# will create position markers in range of 2000 mm to 9000 mm,
converted to cm (20.00 to 90.00) #
# dynamically printed to
PDF
#
##################################################################################################
#createAll()
# Test()
byInput()
def byInput():
sLenght = raw_input("your scale lenght: ")
calculateOffsetFromScaleLenght(float(sLenght))
def Test():
List = [32.6,21.75,21.4,23.45,73,90]
for item in List:
createTest(item)
def createTest(sLenght):
calculateOffsetFromScaleLenght(sLenght)
def createAll():
##########################
# generate scalelenghts #
##########################
i = 20.0
Min = 20
Max = 90
for c in range(Min,Max):
while i < Max:
print i
i += 0.01
calculateOffsetFromScaleLenght(i)
def calculateOffsetFromScaleLenght(initScaleLenght):
offsetList = []
offset = 0
nScaleLenght = initScaleLenght
counter = 0
while counter < markers:
offset = getOffSet(nScaleLenght);
offsetList.append(offset)
nScaleLenght = get_nScalelenght(nScaleLenght,offset)
counter += 1
genPDF(initScaleLenght, (7.62,9.5,12.7),cm, offsetList)
def genPDF(initScaleLenght, (startLine,endLine, stringLine), measure,
offsetList):
##################################
# create reference to PDF object #
##################################
pdf = Canvas(str(initScaleLenght) + '.pdf', bottomup=1)
pdf.setFont("Courier", 10)
###################################
# calculate total lenght of frets #
###################################
fLenght = 0
for item in offsetList:
fLenght += item
#####################
# draw nut #
#####################
pdf.line(startLine*measure,3*measure,endLine*measure,3*measure)
pdf.drawString(stringLine * measure, 3*measure, str(0) + ' /nut/'
+ str(initScaleLenght))
#############################################
# if offsets are generated (valid number) #
#############################################
if len(offsetList) > 0:
pageLenghtMax = 26
accruedOffSet = 0
belowMaxList = []
##############################
# draw terminating rectangle #
##############################
pdf.setStrokeColorRGB(0, 0, 0)
pdf.rect(startLine*measure, 3*measure, 1.9*measure,
(fLenght)*measure, stroke=1, fill=0)
#########################
# build up belowMaxList #
#########################
for o in offsetList:
if o < pageLenghtMax:
belowMaxList.append(o)
###########################
# iterate over offsetList #
###########################
for offset in offsetList:
oIndex = offsetList.index(offset)
########################################
# incremented sum of generated offsets #
########################################
accruedOffSet = offset + accruedOffSet
accruedOffsetString = offset + accruedOffSet
##########################################
# standard markers in red else yellow #
##########################################
if oIndex in indexList:
pdf.setStrokeColorRGB(0, 0, 0)
else:
pdf.setStrokeColorRGB(1, 1, 0)
#####################
# draw the markers #
#####################
if accruedOffSet < pageLenghtMax:
pdf.line(startLine*measure,(accruedOffSet +
3)*measure,endLine*measure,(accruedOffSet + 3)*measure)
pdf.drawString(stringLine * measure, (accruedOffSet +
3)*measure, str(accruedOffSet))
########################################
# create new page if eop is reached #
########################################
else:
pdf.showPage()
pdf.setFont("Courier", 10)
#######################################################
# restart accruedOffSet @ 0 and sraw "connect" marker
#
#######################################################
pdf.line(startLine*measure,3*measure,endLine*measure,
3*measure)
pdf.drawString(stringLine * measure, 3*measure, sJoin)
accruedOffSet = 0
##############################
# draw terminating rectangle #
##############################
pdf.setStrokeColorRGB(0, 0, 0)
pdf.rect(startLine*measure, 3*measure, 1.9*measure,
(accruedOffSet)*measure, stroke=1, fill=0)
################################
# save the data to pdf file #
################################
pdf.save()
print("your template is ready
({0}.pdf)".format(str(initScaleLenght)))
def get_nScalelenght(scalelenght,offset):
newScaleLenght = 0
if scalelenght > 0 and offset > 0:
newScaleLenght = scalelenght - offset
return newScaleLenght
def getOffSet(scaleLenght):
nOffset = 0
if scaleLenght > 0:
nOffset = scaleLenght / n
return nOffset
if '__main__':
main()