Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert inline text to style function #864

Open
creativenoobie opened this issue Sep 1, 2018 · 2 comments
Open

Convert inline text to style function #864

creativenoobie opened this issue Sep 1, 2018 · 2 comments

Comments

@creativenoobie
Copy link

Hi,

I need some help.
I have a string markup which contains my content and Here's my problem:

let markup = "Lorem ipsum dolor sit <strong>amet, consectetur adipiscing elit</strong>. Proin varius dictum magna sit amet dignissim. Donec vitae turpis commodo, interdum turpis vitae, tempus tellus. Nam dictum nunc erat, ac consectetur urna convallis in. Vivamus dignissim sapien nulla, ut elementum <em>massa venenatis sit amet</em>. Nunc lobortis lectus ut metus faucibus convallis. Curabitur nec ante ac magna vulputate bibendum quis a ex. Cras condimentum tempor elementum."

As you can see the string contains html entities such as <strong/> and <em/>, and PDFKIT uses doc.font('bold-font').text(); (http://pdfkit.org/docs/text.html#fonts) in order to insert bold text;

How can I possibly convert all the tags to functions?

@nrkn
Copy link

nrkn commented Sep 3, 2018

Use a package that can parse html (JSDOM or similar)

Assuming your markup is as simple as above (basically flat), you can then just iterate over the parsed nodes and check the nodetype for each node. If it's a text node, just call doc.text( currentNode.nodeValue ), if it's an element type node, check if it's a strong or em and call with the font argument as needed. If the markup is more complex you'll have to figure out how to walk the parsed nodes appropriately.

That should at least give you an idea of how to get started

@naveenkrify
Copy link

Hi,

I need some help.
I have a string markup which contains my content and Here's my problem:

let markup = "Lorem ipsum dolor sit <strong>amet, consectetur adipiscing elit</strong>. Proin varius dictum magna sit amet dignissim. Donec vitae turpis commodo, interdum turpis vitae, tempus tellus. Nam dictum nunc erat, ac consectetur urna convallis in. Vivamus dignissim sapien nulla, ut elementum <em>massa venenatis sit amet</em>. Nunc lobortis lectus ut metus faucibus convallis. Curabitur nec ante ac magna vulputate bibendum quis a ex. Cras condimentum tempor elementum."

As you can see the string contains html entities such as <strong/> and <em/>, and PDFKIT uses doc.font('bold-font').text(); (http://pdfkit.org/docs/text.html#fonts) in order to insert bold text;

How can I possibly convert all the tags to functions?

Have you found any solution to this problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants