57 lines
2.1 KiB
JavaScript
57 lines
2.1 KiB
JavaScript
const fetch = require('node-fetch');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
// 送信先サーバーのURLとAPIキー
|
|
const SERVER_URL = 'https://neo999.next-hd.net:30309/pdfToText'; // サーバーのURLに合わせて変更
|
|
const API_KEY = 'pgqhLFWbDFu4Byz#4afNYX2F6Fa1&$KPjved$8%sUdTQV52caip#EpIKxYUkdd4S'; // サーバーと同じAPIキー
|
|
|
|
// PDFファイルを読み込んでサーバーに送信し、取得したテキストを保存する関数
|
|
async function sendPDFToServer(pdfFilePath, outputTextFilePath) {
|
|
try {
|
|
// PDFファイルを読み込む
|
|
const pdfData = fs.readFileSync(pdfFilePath);
|
|
const fileName = path.basename(pdfFilePath); // ファイル名を取得
|
|
|
|
// PDFデータをBase64エンコード
|
|
const base64PdfData = pdfData.toString('base64');
|
|
// Base64データをJSON形式に変換
|
|
const pdfDataJson = {
|
|
base64: base64PdfData,
|
|
fileName: fileName
|
|
};
|
|
|
|
// サーバーに送信する
|
|
const response = await fetch(SERVER_URL, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'x-api-key': API_KEY
|
|
},
|
|
body: JSON.stringify(pdfDataJson)
|
|
});
|
|
|
|
// サーバーからのレスポンスを確認
|
|
if (!response.ok) {
|
|
throw new Error(`サーバーエラー: ${response.status} ${response.statusText}`);
|
|
}
|
|
|
|
// テキストデータを取得
|
|
const textData = await response.text();
|
|
|
|
// テキストデータをファイルに保存
|
|
fs.writeFileSync(outputTextFilePath, textData, 'utf8');
|
|
console.log(`テキストファイルを保存しました: ${outputTextFilePath}`);
|
|
} catch (error) {
|
|
console.error(`エラーが発生しました: ${error.message}`);
|
|
}
|
|
}
|
|
|
|
// 使用例
|
|
const pdfFilePath = path.join(__dirname, '202505.pdf'); // 読み込むPDFファイルのパス
|
|
const outputTextFilePath = path.join(__dirname, '202505.txt'); // 保存するテキストファイルのパス
|
|
|
|
sendPDFToServer(pdfFilePath, outputTextFilePath);
|
|
|
|
|