OL Learn

Convert txt file to PDF

Maybe I’m missing something, is there a way to directly convert a text file to PDF just in a workflow? I don’t need to modify anything or pull metadata. Just straight convert so another piece of software is able to process it. The other software doesn’t accept text files as input.

Thank you

You need Connect to this as it needs a Template to map the text onto for PDF output.

Thanks hamelj. Can you give me another hint how to do this? I’ve tried doing an extraction with repeat and extraction to try to grab all the lines, but then to put it onto the page I need to use a dynamic table (not a big deal) but I can’t format it correctly as a table. I need to be able to adjust the font to keep the console width for special characters and and spaces. My text file is formatted to look like a report. I just need to put it onto a page in the exact same layout and save as a PDF. Initially I thought I could just use a PDF converter but all the ones I’ve tried mess up formatting as well.

Hi @bgrabau,

I assume that the easiest way to solve your question is to do the following:

  1. Create a Text File based Data Mapping Configuration file.
  2. Add the Table “Lines” to the Data Model (panel) by right clicking on “record” and then selecting the option “Add Table…”.
  3. Add the Field “Line” to the just added Table by right clicking on “Lines” and then selecting the option “Add Field…” (in the Data Model panel).
  4. Add a Action Step to the Steps panel and add the following JavaScript code to the Actions script inside the Step Properties panel:
var txtFile = openTextReader(data.filename);
while((line = txtFile.readLine()) != null) {
	record.tables.Lines.addRow({"Line": line});
}
txtFile.close();
  1. Create a Print Template file.
  2. Replace the Source of the Print Context Section “Section 1” with for example the following HTML code:
<div id="example"></div>
  1. Add a Standard Script to the Standard folder in the Scripts panel.
  2. Add the following JavaScript code to the just created Standard Script:
var result = "";
if("Lines" in record.tables) {
	for(var i = 0; i < record.tables.Lines.length; i++) {
		result += record.tables.Lines[i].fields.Line;
		
		if(i + 1 < record.tables.Lines.length) {
			result += "<br />";
		}
	}
}
results.html(result);

After executing the above steps you will only have to create a Job Creation Preset (optional) and Output Creation Preset (“Generic PDF” Printer Model) via: Connect Designer > File > Print Presets.

After creating all these files you will have to send them to Workflow and after that you will be able to create PDF output by executing the following OL Connect Workflow plugins:

  1. Folder Capture
  2. Execute Data Mapping
  3. Create Print Content
  4. Create Job
  5. Create Output

Actually, I would think the easiest way would be to simply select all text on a page and extract it into a single field. Using a JavaScript statement for the field definition allows the DataMapper to adjust the height of the data selection according to the number of lines in each record:

Then, in the template, display that single field inside a <PRE> element:

<pre class="Field1 ol-scripted">@Field1@</pre><br>

This will preserve the original formatting.

1 Like

Thanks for all the input. Unfortuntately no matter how I go about this the console type font gets changed and the layout gets all screwy. I’ve been able to get it close to the original using an automated print to PDF application which isn’t 100%, but its close. Thanks again for all the input.

You have to make sure you are using a monospace font. It will ensure that all character are using the same space in the PDF.