test.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #!/usr/pkg/bin/python
  2. import os, sys, time
  3. from reportlab import Version as __RL_Version__
  4. from reportlab.graphics.barcode.common import *
  5. from reportlab.graphics.barcode.code39 import *
  6. from reportlab.graphics.barcode.code93 import *
  7. from reportlab.graphics.barcode.code128 import *
  8. from reportlab.graphics.barcode.usps import *
  9. from reportlab.graphics.barcode.usps4s import USPS_4State
  10. from reportlab.graphics.barcode.qr import QrCodeWidget
  11. from reportlab.graphics.barcode.dmtx import DataMatrixWidget, pylibdmtx
  12. from reportlab.platypus import Spacer, SimpleDocTemplate, Table, TableStyle, Preformatted, PageBreak
  13. from reportlab.lib.units import inch, cm
  14. from reportlab.lib import colors
  15. from reportlab.pdfgen.canvas import Canvas
  16. from reportlab.lib.styles import getSampleStyleSheet
  17. from reportlab.platypus.paragraph import Paragraph
  18. from reportlab.platypus.frames import Frame
  19. from reportlab.platypus.flowables import XBox, KeepTogether
  20. from reportlab.graphics.shapes import Drawing, Rect, Line
  21. from reportlab.graphics.barcode import getCodes, getCodeNames, createBarcodeDrawing, createBarcodeImageInMemory
  22. def run():
  23. styles = getSampleStyleSheet()
  24. styleN = styles['Normal']
  25. styleH = styles['Heading1']
  26. story = []
  27. storyAdd = story.append
  28. #for codeNames in code
  29. storyAdd(Paragraph('I2of5', styleN))
  30. storyAdd(I2of5(1234, barWidth = inch*0.02, checksum=0))
  31. storyAdd(Paragraph('MSI', styleN))
  32. storyAdd(MSI(1234))
  33. storyAdd(Paragraph('Codabar', styleN))
  34. storyAdd(Codabar("A012345B", barWidth = inch*0.02))
  35. storyAdd(Paragraph('Code 11', styleN))
  36. storyAdd(Code11("01234545634563"))
  37. storyAdd(Paragraph('Code 39', styleN))
  38. storyAdd(Standard39("A012345B%R"))
  39. storyAdd(Paragraph('Extended Code 39', styleN))
  40. storyAdd(Extended39("A012345B}"))
  41. storyAdd(Paragraph('Code93', styleN))
  42. storyAdd(Standard93("CODE 93"))
  43. storyAdd(Paragraph('Extended Code93', styleN))
  44. storyAdd(Extended93("L@@K! Code 93 :-)")) #, barWidth=0.005 * inch))
  45. storyAdd(Paragraph('Code 128', styleN))
  46. storyAdd(Code128("AB-12345678"))
  47. storyAdd(Paragraph('Code 128 Auto', styleN))
  48. storyAdd(Code128Auto("AB-12345678"))
  49. storyAdd(Paragraph('USPS FIM', styleN))
  50. storyAdd(FIM("A"))
  51. storyAdd(Paragraph('USPS POSTNET', styleN))
  52. storyAdd(POSTNET('78247-1043'))
  53. storyAdd(Paragraph('USPS 4 State', styleN))
  54. storyAdd(USPS_4State('01234567094987654321','01234567891'))
  55. from reportlab.graphics.barcode import createBarcodeDrawing
  56. storyAdd(Paragraph('EAN13', styleN))
  57. storyAdd(createBarcodeDrawing('EAN13', value='123456789012'))
  58. storyAdd(Paragraph('EAN13 quiet=False', styleN))
  59. storyAdd(createBarcodeDrawing('EAN13', value='123456789012', quiet=False))
  60. storyAdd(Paragraph('EAN8', styleN))
  61. storyAdd(createBarcodeDrawing('EAN8', value='1234567'))
  62. storyAdd(PageBreak())
  63. storyAdd(Paragraph('EAN5 price=True', styleN))
  64. storyAdd(createBarcodeDrawing('EAN5', value='11299', price=True))
  65. storyAdd(Paragraph('EAN5 price=True quiet=False', styleN))
  66. storyAdd(createBarcodeDrawing('EAN5', value='11299', price=True, quiet=False))
  67. storyAdd(Paragraph('EAN5 price=False', styleN))
  68. storyAdd(createBarcodeDrawing('EAN5', value='11299', price=False))
  69. storyAdd(Paragraph('ISBN alone', styleN))
  70. storyAdd(createBarcodeDrawing('ISBN', value='9781565924796'))
  71. storyAdd(Paragraph('ISBN with ean5 price', styleN))
  72. storyAdd(createBarcodeDrawing('ISBN', value='9781565924796',price='01299'))
  73. storyAdd(Paragraph('ISBN with ean5 price, quiet=False', styleN))
  74. storyAdd(createBarcodeDrawing('ISBN', value='9781565924796',price='01299',quiet=False))
  75. storyAdd(Paragraph('UPCA', styleN))
  76. storyAdd(createBarcodeDrawing('UPCA', value='03600029145'))
  77. storyAdd(Paragraph('USPS_4State', styleN))
  78. storyAdd(createBarcodeDrawing('USPS_4State', value='01234567094987654321',routing='01234567891'))
  79. storyAdd(Paragraph('QR', styleN))
  80. storyAdd(createBarcodeDrawing('QR', value='01234567094987654321'))
  81. storyAdd(Paragraph('QR', styleN))
  82. storyAdd(createBarcodeDrawing('QR', value='01234567094987654321',x=30,y=50))
  83. def addCross(d,x,y,w=5,h=5, strokeColor='black', strokeWidth=0.5):
  84. w *= 0.5
  85. h *= 0.5
  86. d.add(Line(x-w,y,x+w,y,strokeWidth=0.5,strokeColor=colors.blue))
  87. d.add(Line(x, y-h, x, y+h,strokeWidth=0.5,strokeColor=colors.blue))
  88. storyAdd(Paragraph('QR in drawing at (0,0)', styleN))
  89. d = Drawing(100,100)
  90. d.add(Rect(0,0,100,100,strokeWidth=1,strokeColor=colors.red,fillColor=None))
  91. d.add(QrCodeWidget(value='01234567094987654321'))
  92. storyAdd(d)
  93. storyAdd(Paragraph('QR in drawing at (10,10)', styleN))
  94. d = Drawing(100,100)
  95. d.add(Rect(0,0,100,100,strokeWidth=1,strokeColor=colors.red,fillColor=None))
  96. addCross(d,10,10)
  97. d.add(QrCodeWidget(value='01234567094987654321',x=10,y=10))
  98. storyAdd(d)
  99. storyAdd(Paragraph('Label Size', styleN))
  100. storyAdd(XBox((2.0 + 5.0/8.0)*inch, 1 * inch, '1x2-5/8"'))
  101. storyAdd(Paragraph('Label Size', styleN))
  102. storyAdd(XBox((1.75)*inch, .5 * inch, '1/2x1-3/4"'))
  103. if pylibdmtx:
  104. storyAdd(PageBreak())
  105. storyAdd(Paragraph('DataMatrix in drawing at (10,10)', styleN))
  106. d = Drawing(100,100)
  107. d.add(Rect(0,0,100,100,strokeWidth=1,strokeColor=colors.red,fillColor=None))
  108. addCross(d,10,10)
  109. d.add(DataMatrixWidget(value='1234567890',x=10,y=10))
  110. storyAdd(d)
  111. storyAdd(Paragraph('DataMatrix in drawing at (10,10)', styleN))
  112. d = Drawing(100,100)
  113. d.add(Rect(0,0,100,100,strokeWidth=1,strokeColor=colors.red,fillColor=None))
  114. addCross(d,10,10)
  115. d.add(DataMatrixWidget(value='1234567890',x=10,y=10,color='black',bgColor='lime'))
  116. storyAdd(d)
  117. storyAdd(Paragraph('DataMatrix in drawing at (90,90) anchor=ne', styleN))
  118. d = Drawing(100,100)
  119. d.add(Rect(0,0,100,100,strokeWidth=1,strokeColor=colors.red,fillColor=None))
  120. addCross(d,90,90)
  121. d.add(DataMatrixWidget(value='1234567890',x=90,y=90,color='darkblue',bgColor='yellow', anchor='ne'))
  122. storyAdd(d)
  123. SimpleDocTemplate('out.pdf').build(story)
  124. print('saved out.pdf')
  125. def fullTest(fileName="test_full.pdf"):
  126. """Creates large-ish test document with a variety of parameters"""
  127. story = []
  128. styles = getSampleStyleSheet()
  129. styleN = styles['Normal']
  130. styleH = styles['Heading1']
  131. styleH2 = styles['Heading2']
  132. story = []
  133. story.append(Paragraph('ReportLab %s Barcode Test Suite - full output' % __RL_Version__,styleH))
  134. story.append(Paragraph('Generated at %s' % time.ctime(time.time()), styleN))
  135. story.append(Paragraph('About this document', styleH2))
  136. story.append(Paragraph('History and Status', styleH2))
  137. story.append(Paragraph("""
  138. This is the test suite and docoumentation for the ReportLab open source barcode API.
  139. """, styleN))
  140. story.append(Paragraph("""
  141. Several years ago Ty Sarna contributed a barcode module to the ReportLab community.
  142. Several of the codes were used by him in hiw work and to the best of our knowledge
  143. this was correct. These were written as flowable objects and were available in PDFs,
  144. but not in our graphics framework. However, we had no knowledge of barcodes ourselves
  145. and did not advertise or extend the package.
  146. """, styleN))
  147. story.append(Paragraph("""
  148. We "wrapped" the barcodes to be usable within our graphics framework; they are now available
  149. as Drawing objects which can be rendered to EPS files or bitmaps. For the last 2 years this
  150. has been available in our Diagra and Report Markup Language products. However, we did not
  151. charge separately and use was on an "as is" basis.
  152. """, styleN))
  153. story.append(Paragraph("""
  154. A major licensee of our technology has kindly agreed to part-fund proper productisation
  155. of this code on an open source basis in Q1 2006. This has involved addition of EAN codes
  156. as well as a proper testing program. Henceforth we intend to publicise the code more widely,
  157. gather feedback, accept contributions of code and treat it as "supported".
  158. """, styleN))
  159. story.append(Paragraph("""
  160. This involved making available both downloads and testing resources. This PDF document
  161. is the output of the current test suite. It contains codes you can scan (if you use a nice sharp
  162. laser printer!), and will be extended over coming weeks to include usage examples and notes on
  163. each barcode and how widely tested they are. This is being done through documentation strings in
  164. the barcode objects themselves so should always be up to date.
  165. """, styleN))
  166. story.append(Paragraph('Usage examples', styleH2))
  167. story.append(Paragraph("""
  168. To be completed
  169. """, styleN))
  170. story.append(Paragraph('The codes', styleH2))
  171. story.append(Paragraph("""
  172. Below we show a scannable code from each barcode, with and without human-readable text.
  173. These are magnified about 2x from the natural size done by the original author to aid
  174. inspection. This will be expanded to include several test cases per code, and to add
  175. explanations of checksums. Be aware that (a) if you enter numeric codes which are too
  176. short they may be prefixed for you (e.g. "123" for an 8-digit code becomes "00000123"),
  177. and that the scanned results and readable text will generally include extra checksums
  178. at the end.
  179. """, styleN))
  180. codeNames = getCodeNames()
  181. from reportlab.lib.utils import flatten
  182. width = [float(x[8:]) for x in sys.argv if x.startswith('--width=')]
  183. height = [float(x[9:]) for x in sys.argv if x.startswith('--height=')]
  184. isoScale = [int(x[11:]) for x in sys.argv if x.startswith('--isoscale=')]
  185. options = {}
  186. if width: options['width'] = width[0]
  187. if height: options['height'] = height[0]
  188. if isoScale: options['isoScale'] = isoScale[0]
  189. scales = [x[8:].split(',') for x in sys.argv if x.startswith('--scale=')]
  190. scales = list(map(float,scales and flatten(scales) or [1]))
  191. scales = list(map(float,scales and flatten(scales) or [1]))
  192. for scale in scales:
  193. story.append(PageBreak())
  194. story.append(Paragraph('Scale = %.1f'%scale, styleH2))
  195. story.append(Spacer(36, 12))
  196. for codeName in codeNames:
  197. s = [Paragraph('Code: ' + codeName, styleH2)]
  198. for hr in (0,1):
  199. s.append(Spacer(36, 12))
  200. dr = createBarcodeDrawing(codeName, humanReadable=hr,**options)
  201. dr.renderScale = scale
  202. s.append(dr)
  203. s.append(Spacer(36, 12))
  204. s.append(Paragraph('Barcode should say: ' + dr._bc.value, styleN))
  205. story.append(KeepTogether(s))
  206. SimpleDocTemplate(fileName).build(story)
  207. print('created', fileName)
  208. if __name__=='__main__':
  209. run()
  210. fullTest()
  211. def createSample(name,memory):
  212. f = open(name,'wb')
  213. f.write(memory)
  214. f.close()
  215. createSample('test_cbcim.png',createBarcodeImageInMemory('EAN13', value='123456789012'))
  216. createSample('test_cbcim.gif',createBarcodeImageInMemory('EAN8', value='1234567', format='gif'))
  217. createSample('test_cbcim.pdf',createBarcodeImageInMemory('UPCA', value='03600029145',format='pdf', barHeight=40))
  218. createSample('test_cbcim.tiff',createBarcodeImageInMemory('USPS_4State', value='01234567094987654321',routing='01234567891',format='tiff'))