How can I export an iMessage conversation to .txt or .pdf from Mac? I need to export the chat history of an iMessage chat to either a text or PDF file. 

I realize this is a common question, but I ask it again because preferably, each message should have beside it a record of:


The sender, and 
a time/date-stamp for when the message was sent. 


Is there a way to do this without having to download costly software? I am relatively experienced with Mac software and could potentially navigate scripting (I am most experienced with Python3, but have some limited experience with AppleScript), Terminal, and/or other techniques on the advanced side in this fix if walked through the steps.
First, make an iTunes backup, then find your iTunes backup folder:

macOS

~/Library/Application Support/MobileSync/Backup/


Windows

%appdata%/Roaming/Apple Computer/MobileSync/Backup


There will be one randomly named folder, except you have backed up more devices with iTunes. In that folder, there is database file with your messages.

You can open it using free SQLite database viewer and/or export the contents to CSV file format.



If you have a mac, you can simply


open Messages app
sign in (if you are not)
select the particular conversation
File > Print > Save (output will be in PDF)


The result doesn't look great, but it does contain all the images and the text is searchable.

Your messages database is stored in ~/Library/Messages/ under chat.db, so you can export it  using SQLite database viewer as stated above, but the pictures and other files are stored in ~/Library/Messages/Attachments.