Lasso Soft Inc. > Home

[colGenerator]

LinkcolGenerator
AuthorNikolaj de Fine Licht
CategoryPDF
Version8.x
LicensePublic Domain
Posted29 Oct 2006
Updated29 Oct 2006
More by this author...

Description

This tag is an implementation of the multipage-PDF code by Jolle Carlestam, jolle@carlestam.com, and is an attempt to overcome a couple of typographical and practical shortcomings in PDF-creation with Lasso. I wanted a tag to be able to insert more than one controled text string in a document without repeating the code each time.

It can be seen in use here: Go to www.copenhagenartists.com/artists. Click any artist name, then click Press Service and click the Biography PDF link in the upper left part of the page.

It requires an environment where a PDF_Doc with page margins etc. has been created and a number of vars have been declared and set to a value (see below), and it takes a text string with paragraphs separated by 2 or more line feeds as input. It outputs one column with customizable width and height, and top and left positions determined by a referenced top position var and the left page margin from the environment respectively.

The main features are:
- paragraphs are separated by a customizable space, NOT by an empty line
- orphan control. It does not do widow control yet...
- automatic insertion of pages using a custom made PDF as template, allowing to mimic the use of master pages in layout programmes such as InDesign

Further, the vertical position in pts of each line in the text flow can be shown, useful when debugging positioning issues in a PDF.

Hardcoded into the tag is the printing of the current page no on the bottom centre of the page. Ideally this should be abstracted and turned into an option.

The tag uses a pts-per-cm unit, named $ppcm, for positioning, which allows to set positions in cm instead of points, useful when comparing with mesurements on a printed PDF.

Further development should include:
- widow control
- a 2 column option as in the code by Jolle
- off/on option for page number printing

Sample Usage

First, the pts-per-cm and the page margins must be set:

// amount of points to make 1 cm
var('ppcm'		= decimal(28.584)); 
	
// page margins
var('pagemargin_left'	= math_floor(8 * $ppcm)); 
var('pagemargin_right'	= math_floor(0 * $ppcm)); 
var('pagemargin_top'	= math_floor(5 * $ppcm)); 
var('pagemargin_bottom'	= math_floor(0 * $ppcm)); 

Font and leading must be declared, for example like this:

var('txt_font'		= pdf_font(-file='/site/media/fonts/MeliorLTStd.otf', -size=9, -embed)); 
var('txt_leading' 	= 14); 

A PDF_Doc must be declared, for example like this:

var('pdfDoc' = (pdf_doc(
	-file = 'path_to_file.pdf',
	-size = 'A4', 
	-nocompress, 
	-usedate, 
	-title='MyPDF', 
	-author='MySignature', 
	-margins=(:$pagemargin_left,$pagemargin_right,$pagemargin_top,$pagemargin_bottom)))); 

Further a number of vars must be set for required parameters of the tag:

// vars for positioning and layout controling values etc.
var('vertPos'		= (integer)); 	// top position adding up with each element in the PDF_Doc
var('template'		= (string)); 	// path to pdf-template used for insertion of subsequent pages
var('colwidth'		= (integer)); 	// width of active text column in pts
var('colheight'		= (integer));	// height of active text column in pts

$vertPos		= $pagemargin_top; 
$template		= '/site/media/pdf/A4seq_tmpl.pdf'; 
$colwidth		= math_floor(9.2 * $ppcm);	
$colheight		= math_floor(21 * $ppcm); 

Now the tag can be used like this:

colGenerator($MyTextStringWithParagraphs, 
	-template	= $template, 
	-coltoppos	= $pagemargin_top,  
	-colwidth	= $colwidth, 
	-colheight	= $colheight
	); 
	
Optional parameters are:

-lines_min. Integer. Controls minimum number of lines in last paragraph on page (orphan control). Defaults to two lines.
-space_after. Integer. Controls the space in pts inserted after each paragraph. Defaults to 8 pts.
-showLinePos. No value. When used the vertical position in pts of each line is inserted at the end of the line

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.

(type) == 'string') && (var_defined('pdfDoc')) && (var_defined('ppcm')) && (var_defined('vertPos')) && (var_defined('txt_font')) && (var_defined('txt_leading')))
		); 

		// reference vertical start position var
		// reference used because the current value must be accessible outside the tag
		local('vpos'	= @($vertPos)); 
		
		// position of column end point
		local('colendpos'	= math_add(#coltoppos,#colheight)); 
		
		// default min number of lines in last paragraph on page = 2
		!local('lines_min') 
			? local('lines_min'	= 2); 
		#lines_min		= math_sub(#lines_min,1); // for some reason the usage needs one less than actually displayed
		
		// default space after paragraphs = 8 pts
		!local('space_after') 
			? local('space_after' = 8); 
		
		// show line numbers
		local('linepos'	= 'N'); 
		local_defined('showLinePos') 
			? #linepos	= 'Y'; 
		
		// make spacer object
		local('spaceObj'	= (pdf_text('\r', -font=$txt_font, -leading=#space_after))); 
		
		// declare vars
		local('tmp'			= (array)); 
		local('parWords'	= (array));		// array to hold single words of each paragraph
		local('lineChk'		= (string));	// internal line length check var
		local('textStr'		= (string));	// string holding output content of each paragraph
		local('printObj'	= (string));	// var to hold paragraph for output to print
		local('pageNo'		= (integer)); 	// var to hold page no for print
		
		// normalize line endings
		#input		= string_replaceregexp(#input, -find='( *\r\n)', -replace='\r'); 
		#input		= string_replace(#input, -find='\r\n', -replace='\r'); 
		#input		= string_replace(#input, -find='\n', -replace='\r'); 
		#input		= string_replaceregexp(#input, -find='(\r{2,})', -replace='\r\r'); 
		
		// make array of paragraphs
		#tmp		= #input->(split('\r\r')); 
		
		// iterate paragraphs
		iterate(#tmp, local('i')); 
			if(math_add(#vpos, ($txt_leading * #lines_min)) <= #colendpos); 	// if column end position isnt going to be reached
				#parWords	= #i->(split(' ')); // make array of words in paragraph
				// iterate paragraph words
				iterate(#parWords, local('ii')); 
					if($txt_font->(textwidth(#lineChk + #ii)) < #colwidth); 	// if resulting width is less than colwidth
						#textStr	+= (#ii + ' '); 
						#lineChk	+= (#ii + ' '); 
					else($txt_font->(textwidth(#lineChk + #ii)) == #colwidth);	// if resulting width is equal to colwidth
						if(math_add(#vpos, $txt_leading) < #colendpos); 		// if resulting position is less than column end position
							#linepos		== 'N' 
								? #textStr	+= (#ii + '\r') 
								| #textStr	+= (#ii + ' ' + #vpos + '\r'); 		// insert word, line number N/Y and line feed
							#lineChk	= (string); 
							#vpos		= math_add(#vpos, $txt_leading); 
						else; 													// end of page reached, print what we have and insert new page
							#printObj	= (pdf_text(#textStr, -font=$txt_font, -leading=$txt_leading)); 
							$pdfDoc->(add(#printObj)); 
							$pdfDoc->(insertpage(pdf_read(-file=#template), 1, -newpage = true)); 
							#pageNo		= $pdfDoc->(getpagenumber); 
							$pdfDoc->(drawtext(('- ' + #pageNo + ' -'), -font=$txt_font, -left=math_floor(10.2 * $ppcm), -top=math_floor(28.2 * $ppcm))); 
							#lineChk	= (#ii + ' '); 
							#textStr	= (#ii + ' '); 
							#vpos		= #coltoppos; 
						/if; 
					else; 														// if resulting width is more than colwidth
						if(math_add(#vpos, $txt_leading) < #colendpos); 
							#linepos		== 'N' 
								? #textStr	+= ('\r' + #ii + ' ') 
								| #textStr	+= (' ' + #vpos + '\r' + #ii + ' '); // insert line number N/Y + line feed + word + space
							#lineChk	= (#ii + ' '); 
							#vpos		= math_add(#vpos, $txt_leading); 
						else; 													// end of page reached, print what we have and insert new page
							#printObj		= (pdf_text(#textStr, -font=$txt_font, -leading=$txt_leading)); 
							$pdfDoc->(add(#printObj)); 
							$pdfDoc->(insertpage(pdf_read(-file=#template), 1, -newpage = true)); 
							#pageNo		= $pdfDoc->(getpagenumber); 
							$pdfDoc->(drawtext(('- ' + #pageNo + ' -'), -font=$txt_font, -left=math_floor(10.2 * $ppcm), -top=math_floor(28.2 * $ppcm))); 
							#lineChk	= (#ii + ' '); 
							#textStr	= (#ii + ' '); 
							#vpos		= #coltoppos; 
						/if; 
					/if; 
				/iterate; 
				#printObj	= (pdf_text(#textStr, -font=$txt_font, -leading=$txt_leading)); 
				$pdfDoc->(add(#printObj)); 
				$pdfDoc->(add(#spaceObj)); 
				#vpos		= math_add(#vpos, $txt_leading, #space_after); 
				#lineChk	= (string); 
				#textStr	= (string); 
			else; // else a new paragraph has to start on a new page
				$pdfDoc->(insertpage(pdf_read(-file=#template), 1, -newpage = true)); 
				#pageNo		= $pdfDoc->(getpagenumber); 
				$pdfDoc->(drawtext(('- ' + #pageNo + ' -'), -font=$txt_font, -left=math_floor(10.2 * $ppcm), -top=math_floor(28.2 * $ppcm))); 
				#vpos		= #coltoppos; 
				
				#parWords	= #i->(split(' ')); 
				iterate(#parWords, local('ii')); 
					if($txt_font->(textwidth(#lineChk + #ii)) < #colwidth); 
						#textStr	+= (#ii + ' '); 
						#lineChk	+= (#ii + ' '); 
					else($txt_font->(textwidth(#lineChk + #ii)) == #colwidth); 
						if(math_add(#vpos, $txt_leading) < #colendpos); 
							#linepos		== 'N' 
								? #textStr	+= (#ii + '\r') 
								| #textStr	+= (#ii + ' ' + #vpos + '\r'); 
							#lineChk	= (string); 
							#vpos	= math_add(#vpos, $txt_leading); 
						else; // end of page reached
							#printObj		= (pdf_text(#textStr, -font=$txt_font, -leading=$txt_leading)); 
							$pdfDoc->(add(#printObj)); 
							$pdfDoc->(insertpage(pdf_read(-file=#template), 1, -newpage = true)); 
							#pageNo		= $pdfDoc->(getpagenumber); 
							$pdfDoc->(drawtext(('- ' + #pageNo + ' -'), -font=$txt_font, -left=math_floor(10.2 * $ppcm), -top=math_floor(28.2 * $ppcm))); 
							#lineChk	= (#ii + ' '); 
							#textStr	= (#ii + ' '); 
							#vpos		= #coltoppos; 
						/if; 
					else; 
						if(math_add(#vpos, $txt_leading) < #colendpos); 
							#linepos		== 'N' 
								? #textStr	+= ('\r' + #ii + ' ') 
								| #textStr	+= (' ' + #vpos + '\r' + #ii + ' '); 
							#lineChk	= (#ii + ' '); 
							#vpos		= math_add(#vpos, $txt_leading); 
						else; // end of page reached
							#printObj	= (pdf_text(#textStr, -font=$txt_font, -leading=$txt_leading)); 
							$pdfDoc->(add(#printObj)); 
							$pdfDoc->(insertpage(pdf_read(-file=#template), 1, -newpage = true)); 
							#pageNo		= $pdfDoc->(getpagenumber); 
							$pdfDoc->(drawtext(('- ' + #pageNo + ' -'), -font=$txt_font, -left=math_floor(10.2 * $ppcm), -top=math_floor(28.2 * $ppcm))); 
							#lineChk	= (#ii + ' '); 
							#textStr	= (#ii + ' '); 
							#vpos		= #coltoppos; 
						/if; 
					/if; 
				/iterate; 
				
				#printObj	= (pdf_text(#textStr, -font=$txt_font, -leading=$txt_leading)); 
				$pdfDoc->(add(#printObj)); 
				$pdfDoc->(add(#spaceObj)); 
				#vpos		= math_add(#vpos, $txt_leading, #space_after); 
				#lineChk	= (string); 
				#textStr	= (string); 
			/if; 
		/iterate; 
		
	/define_tag; 

?>

Comments

No comments

Please log in to comment

Subscribe to the LassoTalk mail list

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft