@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()



On Oct 3, 10:11 am, mdipierro <[email protected]> wrote:
> what is scaling?
>
> On Oct 3, 12:02 am, mart <[email protected]> wrote:
>
> > Ok, 'm really liking this! a few questions:
>
> > 1) I'm not catching on where we get pdflatex... is it a plugin? where
> > do we get it?
>
> > 2) Is this a feature that can be add to an up for users? meanin
> > something like "upload your document - then download your pdf (or
> > email generated pdf")? should be doable?
>
> > 3) I used reportlab for drawing fingerboard templates for kids
> > learning the violin (which I got some nasty email from some template
> > maker/vendor, so now I really want to put this out there - its just
> > number crunching so it should be a crome to sell numbers to kids ;) )
> > Is here a way to make use of those drawing capabilities? simple: draw
> > lines @ coordinates?
>
> > 4) scaling: is there a way to control that at this end? (for printing
> > purposes - scaling needs to be disabled)
>
> > thanks :)
>
> > On Oct 3, 12:28 am, mdipierro <[email protected]> wrote:
>
> > > or better
>
> > > {{
> > > import re
> > > from gluon.contrib.markmin.markmin2pdf import markmin2pdf
> > > def markmin_serializer(text,tag=None,attr={}):
> > >     if tag==None: return re.sub('\s+',' ',text)
> > >     if tag=='br': return '\n\n'
> > >     if tag=='h1': return '# '+text+'\n\n'
> > >     if tag=='h2': return '#'*2+' '+text+'\n\n'
> > >     if tag=='h3': return '#'*3+' '+text+'\n\n'
> > >     if tag=='h4': return '#'*4+' '+text+'\n\n'
> > >     if tag=='p': return text+'\n\n'
> > >     if tag=='li': return '\n- '+text.replace('\n',' ')
> > >     if tag=='tr': return text[3:].replace('\n',' ')+'\n'
> > >     if tag in ['table','blockquote']: return '\n-----\n'+text+'\n------
> > > \n'
> > >     if tag in ['td','th']: return ' | '+text
> > >     if tag in ['b','strong','label']: return '**%s**' % text
> > >     if tag in ['em','i']: return "''%s''" % text
> > >     if tag in ['tt','code']: return '``%s``' % text
> > >     if tag=='a': return '[[%s %s]]' % (text,attr.get('_href',''))
> > >     return text
> > > html=response.render('%s/%s.html' %
> > > (request.controller,request.function))
> > > markmin=TAG(html).element('#content').flatten(markmin_serializer)
> > > pdf,warnings,errors=markmin2pdf(markmin)
> > > if errors:
> > >    response.headers['Content-Type']='text'
>
> > > response.write(HTML(BODY(H1('errors'),LU(*errors),H1('warnings'),LU(*warnings))))
> > > else:
> > >    response.headers['Content-Type']='application/pdf'
> > >    response.write(pdf,escape=False)
> > > pass
>
> > > }}
>
> > > On Oct 2, 11:10 pm, mdipierro <[email protected]> wrote:
>
> > > > install pdflatex
>
> > > > create a views/generic.pdf that contains:
>
> > > > ---- begin file ----
> > > > {{
> > > > import re
> > > > from gluon.contrib.markmin.markmin2pdf import markmin2pdf
> > > > def markmin_serializer(text,tag=None,attr={}):
> > > >     if tag==None: return re.sub('\s+',' ',text)
> > > >     if tag=='br': return '\n\n'
> > > >     if tag=='h1': return '# '+text+'\n\n'
> > > >     if tag=='h2': return '#'*2+' '+text+'\n\n'
> > > >     if tag=='h3': return '#'*3+' '+text+'\n\n'
> > > >     if tag=='h4': return '#'*4+' '+text+'\n\n'
> > > >     if tag=='li': return '\n- '+text.replace('\n',' ')
> > > >     if tag=='table': return '\n-----\n'+text+'\n------\n'
> > > >     if tag=='tr': return text[3:].replace('\n',' ')+'\n'
> > > >     if tag=='td': return ' | '+text
> > > >     if tag=='p': return text+'\n\n'
> > > >     if tag=='b' or tag=='strong': return '**%s**' % text
> > > >     if tag=='em' or tag=='i': return "''%s''" % text
> > > >     if tag=='tt' or tag=='code': return '``%s``' % text
> > > >     if tag=='a': return '[[%s %s]]' % (text,attr.get('_href',''))
> > > >     return text
> > > > html=response.render('%s/%s.html' %
> > > > (request.controller,request.function))
> > > > markmin=TAG(html).element('#content').flatten(markmin_serializer)
> > > > response.write(markmin2pdf(markmin)[0],escape=False)
> > > > response.headers['content-type']='text'}}
>
> > > > ---- end file---
>
> > > > Now you should be able to view any web2py page with .pdf in pdf.
>
> > > > I am sure this can also be done and better with pyfpdf but I have not
> > > > tried. Perhaps Mariano can help us
>
>

Reply via email to