diff --git a/src/converters/document.js b/src/converters/document.js index e5389eb..47f2134 100644 --- a/src/converters/document.js +++ b/src/converters/document.js @@ -58,6 +58,12 @@ async function convertHtmlToMd(inputPath, outputPath) { await fs.writeFile(outputPath, markdown); } +async function convertMdToPdf(inputPath, outputPath) { + const markdown = await fs.readFile(inputPath, 'utf8'); + const html = new MarkdownIt().render(markdown); + await renderHtmlToPdf(html, outputPath); +} + async function extractPdfPageTexts(inputPath) { const data = new Uint8Array(await fs.readFile(inputPath)); const doc = await pdfjsLib.getDocument({ data }).promise; @@ -101,6 +107,8 @@ export function registerDocumentConverters() { register({ family: 'document', sourceFormat: 'html', targetFormat: 'pdf', convert: convertHtmlToPdf }); register({ family: 'document', sourceFormat: 'md', targetFormat: 'html', convert: convertMdToHtml }); register({ family: 'document', sourceFormat: 'html', targetFormat: 'md', convert: convertHtmlToMd }); + register({ family: 'document', sourceFormat: 'md', targetFormat: 'pdf', convert: convertMdToPdf }); + register({ family: 'document', sourceFormat: 'pdf', targetFormat: 'md', convert: convertPdfToTxt }); register({ family: 'document', sourceFormat: 'docx', targetFormat: 'pdf', convert: convertDocxToPdf }); register({ family: 'document', sourceFormat: 'pdf', targetFormat: 'txt', convert: convertPdfToTxt }); register({ family: 'document', sourceFormat: 'pdf', targetFormat: 'html', convert: convertPdfToHtml }); diff --git a/test/converters/document.test.js b/test/converters/document.test.js index c01a0ab..ac64af9 100644 --- a/test/converters/document.test.js +++ b/test/converters/document.test.js @@ -100,4 +100,16 @@ describe('document converters', () => { expect(markdown).toContain('# Heading'); expect(markdown).toContain('**bold**'); }); + + it('converts MD to a valid PDF', async () => { + const inputPath = path.join(tmpDir, 'fixture-for-pdf.md'); + await fs.writeFile(inputPath, '# Heading\n\nSome text.'); + const outputPath = path.join(tmpDir, 'from-md.pdf'); + const entry = resolve('md', 'pdf'); + + await entry.convert(inputPath, outputPath); + + const detected = await detectInputMime(outputPath); + expect(detected.mime).toBe('application/pdf'); + }, 20000); }); diff --git a/test/converters/documentFromPdf.test.js b/test/converters/documentFromPdf.test.js index e028938..5108da8 100644 --- a/test/converters/documentFromPdf.test.js +++ b/test/converters/documentFromPdf.test.js @@ -48,6 +48,16 @@ describe('PDF source document converters', () => { expect(html).toContain('

'); }); + it('extracts text from PDF to MD', async () => { + const outputPath = path.join(tmpDir, 'output.md'); + const entry = resolve('pdf', 'md'); + + await entry.convert(pdfFixturePath, outputPath); + + const markdown = await fs.readFile(outputPath, 'utf8'); + expect(markdown).toContain('Extractable fixture text'); + }); + it('reconstructs PDF text into a DOCX (best-effort)', async () => { const outputPath = path.join(tmpDir, 'output.docx'); const entry = resolve('pdf', 'docx');