Convert Lotus Notes Documents to PDFs with LotusScript and Microsoft Word

In some of our old databases we wanted to convert the NotesDocument itself to a PDF-A Documents for archiving purposes.

We did it until now by opening the NotesUIDocument in the client and converted it ot PDF-A with an PDF-Printer (PDF-Creator). But often these NotesUIDocuments did note closed correctly and at the end of the NotesAgent we had thousands of NotesUIDocuments opened in the Client…

We searched for another way to convert the NotesDocument. We tried to render it to an NotesRichtTextItem, converting it to an MimeItem (based on the work from Yuri (here…), generating some HTML. But the HTML had some glitches: Notes inserts a small transparent gif into empty table-cells, so we would have to deal with different MimeEntities wich is also a PITA.

Then I found some code (from Steven J. Knight) in the IBM-Forums wich called a Notes-API function to export Microsoft RTF (which is called by the @Command([ExportFile], „Microsoft RTF“, fileName)

This works as a charm, generating nice looking RTF-Files. Together with a Microsoft Word COM-Instance we convert it to PDA-A quite fast and reliable:

%REM 
  LoggingLibrary PdfConverter 
  Created 11.07.2013 by Harald Reisinger 
  
  Description: 
  Functions to generate RTF and PDF Documents from NotesDocuments 
  Needs the OpenLog Databse for Logging Errors (http://www.openntf.org/internal/home.nsf/project.xsp?action=openDocument&name=OpenLog) 
%END REM 

Option Public 
Option Declare 

Use "OpenLogFunctions" 

Const APIModule = "NNOTES" 
Const wdExportFormatPDF = 17 
Const wdExportFormatXPS = 18 
Const wdExportOptimizeForOnScreen = 1 
Const wdExportOptimizeForPrint = 0 
Const wdExportAllDocument = 0 
Const wdExportCurrentPage = 2 
Const wdExportFromTo = 3 
Const wdExportSelection = 1 
Const wdExportDocumentContent = 0 
Const wdExportDocumentWithMarkup = 7 
Const wdExportCreateHeadingBookmarks = 1 
Const wdExportCreateNoBookmarks = 0 
Const wdExportCreateWordBookmarks = 2 

Declare Function MailGetMessageBodyComposite Lib APIModule Alias "MailGetMessageBodyComposite" ( ByVal hNT As Long, ByVal N As String, ByVal D As String, nD As Long) As Integer 
Declare Function ExportRTF Lib "nxrtf" Alias "ExportRTF" (ByVal sTempFile As String, ByVal flags As Long, hmod As Long, ByVal altlibrary As String, ByVal sRTFFile As String) As Integer 

%REM 
  Sub ConvertDocToRtfFile 

  Description: 
  Converts a NotesDocument to an RTF-Document and saves it under the filePath Generates and removes some temporary Files while Working 
%END REM 

Public Sub ConvertDocToRtfFile(doc As NotesDocument, filePath As String) 
  Dim tempDoc As NotesDocument 
  Dim tempRti As NotesRichTextItem 

  On Error GoTo ErrorHandler 

  Set tempDoc = doc.ParentDatabase.CreateDocument() 
  Set tempRti = tempDoc.CreateRichTextItem("Body") 
  Call doc.RenderToRTItem(tempRti) 
  Call ConvertItemToRtfFile(tempRti, filePath) 
  Exit Sub 

ErrorHandler: 
  Call LogErrorEx("Error in: ConvertDocToRtfFile", SEVERITY_HIGH, doc) 
  Error Err, Error$ 
End Sub 

%REM 
  Sub ConvertDocumentToPdf 

  Description: 
  Converts a NotesDocument to an PDF-Document and saves it under the filePath Needs an COM-Instace of a Word Application created with: 
    Dim word As Variant 
    Set word = CreateObject("Word.Application") 

    Generates and removes some temporary Files while Working 
%END REM 

Public Sub ConvertDocumentToPdf(doc As NotesDocument, word As Variant, filePath As String) 
  Dim wordDoc As Variant 

  On Error GoTo ErrorHandler 
  Call ConvertDocToRtfFile(doc, filePath & ".rtf") 'http://msdn.microsoft.com/en-us/library/office/bb216319(v=office.12).aspx 
  Set wordDoc = word.Documents.Open(filePath & ".rtf", False, True, False) 'http://msdn.microsoft.com/en-us/library/office/bb256835(v=office.12).aspx 
  Call wordDoc.ExportAsFixedFormat(filePath, wdExportFormatPDF, False, wdExportOptimizeForPrint, wdExportAllDocument, 0, 9999999, wdExportDocumentContent, True, True, wdExportCreateHeadingBookmarks, True, True, True, Nothing) 
  Call wordDoc.Close(0) 
  Kill filePath & ".rtf" 
  Exit Sub 

ErrorHandler: 
  Call LogErrorEx("Error in: ConvertDocumentToPdf", SEVERITY_HIGH, doc) 
  Error Err, Error$ 
End Sub 

%REM 
  Function ConvertItemToRtfFile 

  Description: Converts an NotesRichTextItem to an RTF-Document and saves it under the filePath Generates and removes a temporary File while Working 
%END REM 

Public Sub ConvertItemToRtfFile(item As NotesRichTextItem, filePath As String) 
  On Error GoTo ErrorHandler 

  Dim fileSize As Long 
  Dim doc As NotesDocument 

  Set doc = item.Parent 
  Call MailGetMessageBodyComposite(doc.handle , "Body", filePath & ".cd", fileSize) 
  Call ExportRTF(filePath & ".cd", 0, 0, "", filePath) 
  Kill filePath & ".cd" 
  Exit Sub 

ErrorHandler: 
  Call LogErrorEx("Error in: ConvertItemToRtfFile", SEVERITY_MEDIUM, doc) 
  Error Err, Error$ 
End Sub

22 Gedanken zu „Convert Lotus Notes Documents to PDFs with LotusScript and Microsoft Word“

  1. Do you have an specific questions to the setup? This code is used on a HCL Notes Client installed together with Microsoft Word. Additionally, you need the OpenLog.nsf (you can find it on openntf.org) to Log Error Messages, or you can disable the code which calls the OpenLog Functions 🙂

  2. Thank you for the code, but it is not working as expected. Imported Pictures in the Notes Document are not Part of the resulting RTF and PDF. The resulting PDF only contains text.
    Is this a known thing or am I doing something wrong?

  3. Guten Tag, ich benötige die Script-Lib „OpenLogFunctions“, um das o.g. Beispiel zu prüfen, ob es für mein aktuelles Projekt Notes_to_PDF zu verwenden ist. Ich habe schon viele Beispiel-Codes aus dem Internet versucht zu nutzen, die aber das gewünschte Ergebnis nicht gebracht haben.

    Vielen Dank
    mfG Erich

  4. Hi Can you explain to me how to call this code means I need to write any piece of code to call this or how to do this I need to make PDF from notes document

  5. Hallo Erich,

    die Open-Log Bibliothek bzw. Datenbank kannst Du hier finden: https://www.openntf.org/main.nsf/project.xsp?r=project/OpenLog

    Zum Betrieb des Scripts ist die Bibliothek allerdings nicht erforderlich, du kannst einfach alle CodeZeilen die mit „Call LogErrorEx“ auch mit einem einfachen Print „….“ ersetzen. OpenLog erstellt nur schöne Dokumente für jeden Aufruf in einer eigenen Datenbank, die man dann gut ansehen, filtern und gruppieren kann.

    lg
    Harald

  6. Hello Amar,

    you can copy this code in an LotusScript Library. It depends on your use-case how you call it from your code (with a LotusScript Agent or with a Click to an Actionbutton). You have to Call to „ConvertDocToRtfFile“ Method with two Parameters, one is an Instance of the NotesDocument-Object you want to convert, and the second Parameter is the File-Path where the PDF-File should be stored.

    You need some programming practice in HCL Notes / Domino to integrate this.

    Best regards

    Harald

  7. Hi First I copy this in one script Lib and then write one agent and call this script lib
    Sub Initialize
    Dim Session As New NotesSession

    Set objWord = CreateObject(„Word.Application“)
    Set db = session.Currentdatabase
    Set collection = db.Unprocesseddocuments
    If collection.count = 0 Then
    Exit Sub
    End If
    Set doc = collection.Getfirstdocument()
    Do Until doc Is Nothing
    filePath = „C:\TEMP\“ & doc.Universalid
    ‚Call ConvertDocToRtfFile(doc , filePath)
    Call ConvertDocumentToPdf(doc, objWord, filePath)

    Set doc = collection.Getnextdocument(doc)
    Loop
    End Sub
    But problem is that PDF created but no value is there

  8. No Nothing error come Only PDF created but no data is there any Lib I need to import if possible can we connect in Teams

  9. Hi,

    either you can use the OpenLog Library, or you remove/comment all references to that Lib:

    ' Call LogErrorEx("Error in: ConvertDocToRtfFile", SEVERITY_HIGH, doc)

    or

    ' Use "OpenLogFunctions"

    All other Functions are builtin in Notes

  10. Hi Harald,

    I have not used this OpenLog Library, comment this Call Log one PDF created but no data appear in the PDF this is surprised

  11. Harald,

    I placed your code into a view action button in a Notes 11FP2 Windows client running Domino 11FP2 on Windows 2012 R2 Server.

    The code ran without error (both runtime on the client and no errors logged in either log.nsf -OR- OpenLog 1.5).

    Both PDF files that my code produces, were created from each document in my view with most files sizes being 20-25KB in size. Since I’m unable to open up any of the created files, I’m not able to determine whether or not data actually exists.

    My issue is that neither Adobe DC client can open the file NOR can Cute PDF Professional can open the files.

    My Adobe Acrobat Pro DC 2015 Release (Classic) version 2015.006.30527 yells „Adobe Acrobat DC could not open ‚34673-1.pdf‘ because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn’t correctly decoded).
    To create an Adobe PDF document, go to the source application. Then print the document to Adobe PDF.“

    Cute PDF Professional version 4.0 simply states „Invalid PDF File!“

    My view Action Button LotusScript code is:

    Sub Click(Source As Button)
    Dim session As New NotesSession
    Dim db As NotesDatabase
    Dim view As NotesView
    Dim doc As NotesDocument
    Dim coversheetDoc As NotesDocument
    Dim coreDumpDoc As NotesDocument
    Dim pdfPath As String
    Dim counter As Integer

    ‚ Set the database path
    Set db = session.CurrentDatabase

    ‚ Set the view to process
    Set view = db.GetView(„vw_ByLastName“) ‚ Replace „YourViewName“ with the actual name of your view

    ‚ Get the total number of documents in the view
    Dim totalDocs As Integer
    totalDocs = view.EntryCount

    ‚ Initialize the counter
    counter = 0

    ‚ Loop through all documents in the view
    Set doc = view.GetFirstDocument()
    Do Until doc Is Nothing
    ‚ Increment the counter
    counter = counter + 1

    ‚ Print status statement
    Print „Now processing “ & counter & “ of “ & totalDocs & “ documents“

    ‚ Compose ‚coversheet‘ form in ‚Portrait‘ layout
    Set coversheetDoc = db.CreateDocument()
    Call coversheetDoc.ReplaceItemValue(„Form“, „coversheet“)
    Call coversheetDoc.ReplaceItemValue(„MemberIDTX“, doc.MemberIDTX(0)) ‚ Assuming MemberIDTX is a text field
    Call coversheetDoc.ReplaceItemValue(„$PaperOrientation“, „1“) ‚ Portrait layout

    ‚ Save ‚coversheet‘ as PDF
    pdfPath = „c:\mcl\updates\“ & doc.MemberIDTX(0) & „-1.pdf“
    Call ConvertDocToRtfFile(coversheetDoc, pdfPath)

    ‚ Compose ‚CoreDump‘ form in ‚Landscape‘ layout
    Set coreDumpDoc = db.CreateDocument()
    Call coreDumpDoc.ReplaceItemValue(„Form“, „CoreDump“)
    Call coreDumpDoc.ReplaceItemValue(„MemberIDTX“, doc.MemberIDTX(0)) ‚ Assuming MemberIDTX is a text field
    Call coreDumpDoc.ReplaceItemValue(„$PaperOrientation“, „2“) ‚ Landscape layout

    ‚ Save ‚CoreDump‘ as PDF
    pdfPath = „c:\mcl\updates\“ & doc.MemberIDTX(0) & „-2.pdf“
    Call ConvertDocToRtfFile(coreDumpDoc, pdfPath)

    ‚ Get the next document in the view
    Set doc = view.GetNextDocument(doc)
    Loop

    ‚ Print completion message
    Print „Processing complete“

    End Sub

    Not sure if you’ve seen these error types at runtime before, but with no specific errors being thrown and caught by logging is a bit of a challenge.

    Do you have any guidance or insight as to where I need look?

    Thanks for the support

    Rick

  12. Hello Rick,

    I am suggesting to try to comment following Line in my Code Kill filePath & ".rtf". This leaves the intermediate .rtf file on Disk, so you can try to open it manually with your Word-Application, to determine if the Problem is the Export from Notes To Word or between Word To PDF.

    best regards

    Harald

Schreibe einen Kommentar

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.