Added support to print multiple labels at once
This commit is contained in:
		
							parent
							
								
									ee1d8d953a
								
							
						
					
					
						commit
						fcbe180d27
					
				
					 1 changed files with 19 additions and 9 deletions
				
			
		
							
								
								
									
										28
									
								
								pdf2tspl.py
									
										
									
									
									
								
							
							
						
						
									
										28
									
								
								pdf2tspl.py
									
										
									
									
									
								
							|  | @ -59,6 +59,7 @@ def pdf2tspl(filename, labelwidth_mm=100, labelheight_mm=150, dpi=203.2): | ||||||
|     return tspl |     return tspl | ||||||
| 
 | 
 | ||||||
| if __name__ == "__main__": | if __name__ == "__main__": | ||||||
|  |     from PyPDF2 import PdfFileWriter, PdfFileReader | ||||||
|     import argparse |     import argparse | ||||||
|     import sys |     import sys | ||||||
|     parser = argparse.ArgumentParser(description='Convert a PDF to TSPL to send to a label printer.') |     parser = argparse.ArgumentParser(description='Convert a PDF to TSPL to send to a label printer.') | ||||||
|  | @ -70,13 +71,22 @@ if __name__ == "__main__": | ||||||
|     parser.add_argument('-d', '--dpi', type=float, default=203.2, help='Resolution of the printer. Defaults to 8 dots per mm (203.2 dpi)') |     parser.add_argument('-d', '--dpi', type=float, default=203.2, help='Resolution of the printer. Defaults to 8 dots per mm (203.2 dpi)') | ||||||
|     args = parser.parse_args() |     args = parser.parse_args() | ||||||
| 
 | 
 | ||||||
|     tspl = pdf2tspl(args.pdf_file, |     inputpdf = PdfFileReader(open(args.pdf_file, "rb")) | ||||||
|                     labelwidth_mm=args.width, |  | ||||||
|                     labelheight_mm=args.height, |  | ||||||
|                     dpi=args.dpi) |  | ||||||
| 
 | 
 | ||||||
|     if args.tspl_file == '-': |     for i in range(inputpdf.numPages): | ||||||
|         sys.stdout.buffer.write(tspl) |         output = PdfFileWriter() | ||||||
|     else: |         output.addPage(inputpdf.getPage(i)) | ||||||
|         with open(args.tspl_file, 'wb') as fp: |         temp_pdf_name = "temp/document-page%s.pdf" % i | ||||||
|             fp.write(tspl) |         with open(temp_pdf_name, "wb") as outputStream: | ||||||
|  |             output.write(outputStream) | ||||||
|  | 
 | ||||||
|  |         tspl = pdf2tspl(temp_pdf_name, | ||||||
|  |         labelwidth_mm=args.width, | ||||||
|  |         labelheight_mm=args.height, | ||||||
|  |         dpi=args.dpi) | ||||||
|  | 
 | ||||||
|  |         if args.tspl_file == '-': | ||||||
|  |             sys.stdout.buffer.write(tspl) | ||||||
|  |         else: | ||||||
|  |             with open(args.tspl_file, 'wb') as fp: | ||||||
|  |                 fp.write(tspl) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue