Ask Your Question
0

Receipt or order form consecutive numbering plus barcode.

asked 2020-12-10 20:42:40 +0100

WMED gravatar image

When printing multiple identical single sheet documents in Libre writer each sheet would have a consecutive number in the upper right corner and a bar code corresponding to same number at the bottom of the same document. For example an order form, or receipt . Is this possible with any of the LibreOffice aps?

edit retag flag offensive close merge delete

Comments

Is your question exclusively about Writer (you tagged writer) or any other components (you also tagged common) as your question suggests.

Note that your question is meaningless for Draw and Math.

Edit your question to explain what you mean by "printing multiple identical single sheet documents". Do you send a single document to the printer requesting several copies (this is handled by the printer driver)?

Do you mean your document has a single page (kind of template) and you want to generate several different (at least by the number) particular pages from this model?

AskLO is a Question & Answer site and has no conversation or thread. Please take care to edit and not answer because answers are reserved for solutions. Also retag to remove the ambiguity between common and writer.

Don't forget to mention your OS and LO version.

ajlittoz gravatar imageajlittoz ( 2020-12-10 21:02:06 +0100 )edit

true what is said above. If reframed as: is there a route to achieve this, then python might offer such a route:

see https://pypi.org/project/python-barcode/ to generate a barcode.png

def test(): # to insert barcode in an already opened document (preferably cursor is set to a bookmark)
    import uno
    from com.sun.star.awt import Size
    document = XSCRIPTCONTEXT.getDocument() 
    size = Size()
    path = uno.systemPathToFileUrl('~/barcode.png')
    draw_page = document.DrawPage
    image = document.createInstance( 'com.sun.star.drawing.GraphicObjectShape')
    image.GraphicURL = path
    draw_page.add(image)
    size.Width = 2500
    size.Height = 1000
    image.setSize(size)
    image.setPropertyValue('AnchorType', 'AT_FRAME')

Plus some work to get LO running with python, apart from security/safety issues to keep a (non-editable/binary?) macro-incremented number in a file

parsely gravatar imageparsely ( 2020-12-10 22:31:20 +0100 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2020-12-16 08:10:09 +0100

parsely gravatar image

updated 2020-12-20 23:40:18 +0100

Thanks to Lupp (how to load an image as bitmap) this question could now be answered, for .odt and .ods files This is specific autoincrement, check extensions to see if today extensions are present for this or other needs, an extension will be more convenient to get working.

preparations:
- install python 3.x (2.x is no longer supported) for your OS, link text, if not present
- enable python in LO PyUNO
- do the required pip installs according to pypi website

- create a file bartemplate.odt containing a frame where:
- framename = frameBarcode (properties / options)
- size = protected
- borders = none (properties / borders)
- margins = 0

attach the macro to a menu item or keyboard combination

import uno 
ctx = uno.getComponentContext()
smgr = ctx.ServiceManager
StarDeskTop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)

def insertbarcode():

    #https://pypi.org/project/python-barcode/
    #https://python-barcode.readthedocs.io/en/stable/barcode.html
    #https://pypi.org/project/qrtools/

    import barcode
    import os
    from barcode.writer import ImageWriter

    cwd = os.getcwd()                               # get operating system working directory, LO is launched from
    autoIncrementFileName = "barcode.bin"         # file to save autoincrement number
    fileTemplate = "bartemplate.odt"                      # kind of template file with frame, for .ods see below
    barcodeNameOfPNG = "barcode"
    barcodeImageFile = uno.systemPathToFileUrl( os.path.join( cwd , barcodeNameOfPNG + ".png") )

    bitMapName = "foo"             # arbitrary internal name assigned to the bitmap
    target = "frameBarcode"       # name of frame where barcode is inserted

    numberInit = 1001

    if os.path.exists(autoIncrementFileName):
        pass
    else:
        binNum = str(numberInit).encode("utf-8")
        fp = open( autoIncrementFileName, "wb" )
        fp.write( binNum )
        fp.close()

    fp = open( autoIncrementFileName ,  "rb")
    buffr = fp . read ( )
    fp . close ()

    numberUsed = int ( buffr.decode("utf-8") )

    binNum = str(numberUsed + 1).encode("utf-8")
    fp = open( autoIncrementFileName, "wb" )
    fp.write( binNum )
    fp.close()

#   generate barcode file as a PNG of 414 * 280 px

    strNumber = ( 7*"0" + str(numberUsed) ) [-7:]  #adapt 7 to 12 for ean13
    ean = barcode.get( "ean8" , strNumber , writer=ImageWriter())
    eanFullCode = ean.get_fullcode()  # fullcode() adds a 8th checksum digit
    ean.save(barcodeNameOfPNG)        # save in working dir, code forced extension is .png

#   load template file and store as file with ean-number as name 

    fLoad = uno.systemPathToFileUrl( os.path.join( cwd , fileTemplate) )
    document = StarDeskTop.loadComponentFromURL( fLoad , "_blank", 0, () ) 
    fNew = uno.systemPathToFileUrl( os.path.join( cwd , eanFullCode + ".odt" ) )
    document.storeAsURL( fNew , () )  

#   insert barcode in document

    bitMaps = document.createInstance( "com.sun.star.drawing.BitmapTable" )
    bitMaps.insertByName( bitMapName, barcodeImageFile )
    internalName = bitMaps.getByName(bitMapName)

# now a textFrame, but by analogy also .getTextTables (getCellByName() ) and so on

    frame = document.getTextFrames().getByName("frameBarcode")
    frame.BackGraphic = internalName
    frame.FillBitmapMode = 'STRETCH'
    frame.FillBitmapRectanglePoint = 'MIDDLE_MIDDLE'
    frame.FillStyle   = 'BITMAP'


# the following is  for spreadsheet files:
# adapt bartemplate.odt to bartemplate.ods as well as storeToURL( to .ods) 
# and replace code starting from 

#   insert barcode in document in cell B3

#    cellByName = str.upper('b3')

#    i = 'ABCDEFGHIJKLMNOP'.find(cellByName[0:1])
#    j = int(cellByName[1:]) - 1
#    frame = document.getSheets().getByIndex(0)
#    cell = frame.getCellByPosition(i,j)  # = B3   

#   docFrame = document.getCurrentController().getFrame()
#    args = dictToProperty({ "ToPoint" : cellByName})         #more secure is $B$3      
#    dispatcher.executeDispatch(docFrame, ".uno:GoToCell", "", 0, args)

#    args = dictToProperty( {"FileName" : barcodeImageFile  , "FilterName" :  "PNG - Portable Network Graphic" , "AsLink" : False})
#    dispatcher.executeDispatch( docFrame , ".uno:InsertGraphic ...
(more)
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

2 followers

Stats

Asked: 2020-12-10 20:42:40 +0100

Seen: 88 times

Last updated: Dec 20 '20