The document follows consistent rules, if you look at it more closely.
Firstly, the document itself is actually highly cursive, both in its kanji and hiragana. After this time and up until 1945, it became standard for treaties and formal documents to be written exclusively in kanji and katakana. The fact that this uses hiragana is a result of its cursive style.
But, as we can see, katakana is used to write the foreign name Matthew Calbraith Perry, as katakana is the only standard script for writing foreign names, even in the otherwise cursive script of the document.
Some of the characters that look like katakana, such as ハ, are actually hentaigana, as @marasai points out, referencing this list. In fact, if you reference all the documents here (courtesy of @viven), all of which are cursive, all characters used are perfectly valid hentaigana. Some look like katakana, but are actually consistently hiragana throughout the document, but are simply historical variant hiragana that have since become nonstandard. Hiragana was only standardized in 1900 (this document is from 1854), making these historical variants understandable.
Note that all the kana are hiragana/hentaigana. We see the hentaigana ハ appear again on both pages
As mentioned, the sole exception to this is when writing a foreigner's name, which has always been standard to write in katakana throughout history, even in cursive documents.
The document isn't haphazard persay, but rather uses a very cursive style, meaning that all kanji are cursive, and all kana are hiragana (including variant hentaigana) except for foreign names. Its style is totally consistent in this regard, just different to modern Japanese usage, particularly of hiragana/hentaigana forms.