Gir*_* SB 3 html javascript plugins rtf
我有一个带有页眉和页脚的输入 HTML 文件。它需要转换为RTF。HTML 的页眉/页脚应在生成的 RTF 文件中重复。
是否有任何插件可以仅使用JavaScript将HTML转换为 RTF?
你可以使用这个转换器
但是它没有解决要点(ul,li 元素)
function convertHtmlToRtf(html) {
if (!(typeof html === "string" && html)) {
return null;
}
var tmpRichText, hasHyperlinks;
var richText = html;
// Singleton tags
richText = richText.replace(/<(?:hr)(?:\s+[^>]*)?\s*[\/]?>/ig, "{\\pard \\brdrb \\brdrs \\brdrw10 \\brsp20 \\par}\n{\\pard\\par}\n");
richText = richText.replace(/<(?:br)(?:\s+[^>]*)?\s*[\/]?>/ig, "{\\pard\\par}\n");
// Empty tags
richText = richText.replace(/<(?:p|div|section|article)(?:\s+[^>]*)?\s*[\/]>/ig, "{\\pard\\par}\n");
richText = richText.replace(/<(?:[^>]+)\/>/g, "");
// Hyperlinks
richText = richText.replace(
/<a(?:\s+[^>]*)?(?:\s+href=(["'])(?:javascript:void\(0?\);?|#|return false;?|void\(0?\);?|)\1)(?:\s+[^>]*)?>/ig,
"{{{\n");
tmpRichText = richText;
richText = richText.replace(
/<a(?:\s+[^>]*)?(?:\s+href=(["'])(.+)\1)(?:\s+[^>]*)?>/ig,
"{\\field{\\*\\fldinst{HYPERLINK\n \"$2\"\n}}{\\fldrslt{\\ul\\cf1\n");
hasHyperlinks = richText !== tmpRichText;
richText = richText.replace(/<a(?:\s+[^>]*)?>/ig, "{{{\n");
richText = richText.replace(/<\/a(?:\s+[^>]*)?>/ig, "\n}}}");
// Start tags
richText = richText.replace(/<(?:b|strong)(?:\s+[^>]*)?>/ig, "{\\b\n");
richText = richText.replace(/<(?:i|em)(?:\s+[^>]*)?>/ig, "{\\i\n");
richText = richText.replace(/<(?:u|ins)(?:\s+[^>]*)?>/ig, "{\\ul\n");
richText = richText.replace(/<(?:strike|del)(?:\s+[^>]*)?>/ig, "{\\strike\n");
richText = richText.replace(/<sup(?:\s+[^>]*)?>/ig, "{\\super\n");
richText = richText.replace(/<sub(?:\s+[^>]*)?>/ig, "{\\sub\n");
richText = richText.replace(/<(?:p|div|section|article)(?:\s+[^>]*)?>/ig, "{\\pard\n");
// End tags
richText = richText.replace(/<\/(?:p|div|section|article)(?:\s+[^>]*)?>/ig, "\n\\par}\n");
richText = richText.replace(/<\/(?:b|strong|i|em|u|ins|strike|del|sup|sub)(?:\s+[^>]*)?>/ig, "\n}");
// Strip any other remaining HTML tags [but leave their contents]
richText = richText.replace(/<(?:[^>]+)>/g, "");
// Prefix and suffix the rich text with the necessary syntax
richText =
"{\\rtf1\\ansi\n" + (hasHyperlinks ? "{\\colortbl\n;\n\\red0\\green0\\blue255;\n}\n" : "") + richText + "\n}";
return richText;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
经过一番搜索后,我找到了一个可行的解决方案:
https://www.npmjs.com/package/html-to-rtf
转换起来html-to-rtf很简单(下面是一段基于browserify的代码):
var htmlToRtf = require('html-to-rtf');
var htmlText = "<div>...</div>"; //or whatever html you want to transform
var htmlAsRtf = htmlToRtf.convertHtmlToRtf(htmlText); // html transformed to rtf
Run Code Online (Sandbox Code Playgroud)
这个解决方案对我有用。js如果没有 browserify,您将必须在下载的模块中找到隐含的内容npm并将它们链接到您的 html 页面。
| 归档时间: |
|
| 查看次数: |
9434 次 |
| 最近记录: |