Convert Markdown String to PDF Page with options

Using the Convert class, am I able to control the generated page’s font at all? I don’t see any options for it.

const markdownBuffer = Buffer.from(markdownString);
await PDFNet.Convert.toPdfWithBuffer(doc, markdownBuffer, ‘.md’);

In general, what is my best approach to take a string of MarkDown and convert it to a PDF page, with font options?

Thank you

Thank you for contacting us about this. You can use the StreamingPDFConversion API to add font face information (along with size, etc).

Refer to the code below:

try {
           // Add a dictionary
        const set = await PDFNet.ObjSet.create();
        const options = await set.createDict();

        // Put options
        await options.putNumber('FontSize', 15);
        await options.putName('FontFace', 'Ariel'); // font face
        await options.putString('FileExtension', '.md'); // add extension here

        const filterFromBuffer = await PDFNet.Filter.createFromMemory(data); // load buffer 

        const documentConversion = await PDFNet.Convert.streamingPdfConversionWithFilter(filterFromBuffer, options);
        console.log('test');
        while (await documentConversion.getConversionStatus() == 1) { // conversion incomplete
            console.log('converting page');
            await documentConversion.convertNextPage();  
        }

        if (await documentConversion.getConversionStatus() == 0) { // conversion success
            console.log('done. saving doc');
            const pdfFile = await documentConversion.getDoc()
            await pdfFile.save("./output.pdf", PDFNet.SDFDoc.SaveOptions.e_linearized); // optionally you can save the document to a filter too
        }
        else {
          console.log('conversion error');
          console.log(await documentConversion.getErrorString())
        }
      } catch (err) {
        console.log(err);
      }

    console.log('Done.'); 

Please let me know if this works for you.

Thank you, I’ll give it a try. But just looking at your code, pdfDoc is unused… Would I have to run a copy page operation on the finished conversion pdfFile and append those manually in a loop? It would be really great if the API could be updated to just provide options to Convert.toPdfWithBuffer :confused: Also, if this works, the documentation for Convert.streamingPdfConversionWithFilter should be updated to include all the file types it can handle.

Yea, I don’t think the filter can accept Markdown:

conversion error
document layout failed: Exception: 
         Message: Invalid UTF8 data
         Conditional expression: (utf8In[i] & UINT32_C(0xC0)) == UINT32_C(0x80)
         Version      : 9.4.0-29d3f4dfa4
         Platform     : OSX
         Architecture : Arm
         Filename     : UnicodeUtils.cpp
         Function     : CodePoint_from_UTF8_Multi
         Linenumber   : 1503

Apologies for the delay. I was able to reproduce a similar error and have forwarded this to the team for further investigation. I will get back to you as soon as I have an update on this.

Thank you, but further help is not needed. I was able to solve this via help in a support ticket ( #46546). I was using the wrong method on Convert; fromTextWithBuffer allows an options dictionary as 3rd param.

The solution snippet:

    const optionsObjSet = await PDFNet.ObjSet.create();
    const optionsDict = await optionsObjSet.createDict();
    await optionsDict.putString('FontFace', 'Helvetica');

    const markdownBuffer = Buffer.from(attachmentPage?.markdown);

    await PDFNet.Convert.fromTextWithBuffer(
      filledDoc,
      markdownBuffer,
      optionsDict
    );