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
  Library 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

 

19 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

Schreibe einen Kommentar

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