Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
目鏈接地址:https://github.com/stonehank/html-to-md
一個用于轉換HTML為Markdown的工具。
可以獲取任意的網頁內容轉為 markdown 之后保存在自己的倉庫
注意:只有有效規范的 HTML 文本才能準確顯示結果,如<p>abc< ,<i>abc</>等都是無效文本
live-demo: https://stonehank.github.io/html-to-md/
npm -i html-to-md
const html2md = require('html-to-md')
// or if you're using ES6
import html2md from 'html-to-md'
console.log(
html2md('<strong><em>strong and italic</em></strong>', options, force)
)
// ***strong and italic***
名稱 | 數據類型 | 默認值 | 說明 |
skipTags | Array |
| 需要忽略的標簽名 |
emptyTags | Array | [] | 不僅忽略它本身,它內部所有標簽名全部忽略 |
ignoreTags | Array |
| 忽視標簽及其內部所有內容 |
aliasTags | Object |
| 為標簽定義一個別名(通常作用于一些不常用標簽) |
renderCustomTags | Boolean | true | 自定義當前標簽部分屬性配置 |
tagListener | Function | (props: TagListenerProps): TagListenerReturnProps => props | 定義是否渲染自定義標簽(非HTML標簽),
|
優先權:skipTags > emptyTags > ignoreTags > aliasTags
例:
html2md('<><b><i>abc</i></b></>', { ignoreTags: [''] })
// ''
html2md('<><b><i>abc</i></b></>', { skipTags: [''] })
// ***abc***
html2md('<><b><i>abc</i></b></>', { emptyTags: [''] })
// abc
html2md('<><b><i>abc</i></b></>', {
skipTags: [''],
aliasTags: { b: 'ul', i: 'li' },
})
// * abc
html2md('<test><b><i>abc</i></b></test>', { renderCustomTags: 'SKIP' })
// ***abc***
值 | 說明 |
true | 表示強制使用自定義配置 |
false | 對自定義配置使用Object.assign操作 |
例:
// 默認 skipTags 為 ['div','html','body']
// 配置一:
html2md('<div><b><i>abc</i></b></div>', { skipTags: ['b'] }, false)
// skipTags 為 ['div','html','body','b']
// 配置二:
html2md('<div><b><i>abc</i></b></div>', { skipTags: ['b'] }, true)
// 經過配置后 skipTags 為 ['b']
key | 說明 |
parentTag | 父標簽名,沒有則為null |
prevTagName | 上一個標簽名,沒有則為null |
nextTagName | 下一個標簽名,沒有則為null |
isFirstSubTag | 是否當前父標簽內部的第一個子標簽 |
attrs | 當前標簽的attributes,以object集合方式,例如 { src, href ... } |
innerHTML | 內部HTML字符串 |
match | 當前的HTML對應Markdown的匹配符號 |
language? | 當前標簽語言,只在 pre 標簽中出現 |
isSelfClosing | 是否自閉和標簽 |
key | 說明 |
attrs | 當前標簽的attributes,以object集合方式,例如 { src, href ... } |
match | 返回一個新的自定義匹配符號 |
language? | 返回自定義pre標簽的language |
擬機為我們提供了一個無限可能的世界。在這個世界里,我們可以嘗試、學習、甚至犯錯,而不必擔心現實生活中的后果。Emacs,作為一款強大的文本編輯器,它的Org模式更是讓人們在組織復雜信息時如魚得水。但是,當我們嘗試將Org文件轉換為HTML格式時,有時會遇到一個令人困惑的問題:“source block missing language specification #include”。這是什么意思?它又該如何解決呢?
在深入探討之前,讓我們先來思考一個問題:為什么我們要將Org文件轉換為HTML?答案可能各有不同,但一個共同點是,我們都希望能夠更好地分享和展示我們的工作。HTML作為一種廣泛支持的格式,能夠讓我們的內容在不同的平臺上呈現出一致的效果。這就像是我們穿上了一件合身的外衣,讓內在的價值得以外顯。
當我們遇到“source block missing language specification #include”的提示時,我們應該怎么辦呢?首先,我們需要理解這個提示的含義。在Emacs的Org模式中,當我們使用源代碼塊時,通常需要指定語言類型,比如#+BEGIN_SRC python。這樣,Emacs就能夠知道如何高亮和格式化我們的代碼。但如果我們忘記了這一步,就會收到上述的提示。
解決這個問題的方法其實很簡單。我們只需要回到源代碼塊的開始,確保正確地指定了語言類型。如果是包含在文件中的代碼,比如C語言的頭文件,我們可以使用#+INCLUDE: "file.h" src c這樣的語法來引入。
通過這個簡單的修正,我們就能夠順利地將Org文件轉換為HTML,而不會再有任何阻礙。這個過程就像是我們在虛擬世界中解決了一個謎題,獲得了前進的鑰匙。
并且可以嘗試以下幾種方法來解決:
方法一:添加語言規范
在 Org 文檔中,對于包含代碼塊的文本,需要添加語言規范,以便 Org-mode 正確地解析和轉換代碼。
具體步驟如下:
在代碼塊的開頭,添加一行指示語言規范的注釋。例如,對于 C 語言代碼,可以添加以下注釋:
#lang c
再次嘗試將 Org 文檔轉換為 HTML。
方法二:使用 org-babel
org-babel 是 Org-mode 的一個擴展包,可以用于將代碼塊轉換為各種格式,包括 HTML。
具體步驟如下:
安裝 org-babel 擴展包。
M-x package-install RET org-babel
在 Org 文檔中,將代碼塊標記為 babel 塊。例如,對于 C 語言代碼,可以使用以下標記:
#+begin_src c#include <stdio.h>int main() { printf("Hello, world!\n"); return 0;}#+end_src
再次嘗試將 Org 文檔轉換為 HTML。
方法三:使用 org-html-export-filter
org-html-export-filter 是 Org-mode 的一個函數,可以用于在導出 HTML 時自定義代碼塊的轉換。
具體步驟如下:
在 Emacs 配置文件中,添加以下代碼:
(defun my-org-html-export-filter (block) "自定義代碼塊的轉換。" (if (string-match-p "^#lang\s+c\s*$" (org-block-property block :language)) (org-html-export-filter-block-as-code block "c" "highlight") (org-html-export-filter-block-as-verbatim block)))(add-hook 'org-html-export-filter-alist 'my-org-html-export-filter)
再次嘗試將 Org 文檔轉換為 HTML。
以上三種方法都可以解決 Org 轉為 HTML 時提示“source block missing language specification #include<stdio.h>”的問題。具體選擇哪種方法,可以根據自己的實際情況和需求來決定。
.NET的SelectPdf Html到Pdf轉換器-社區版是.NET的SelectPdf庫中提供的功能強大的html到pdf轉換器的免費版本。
轉換器提供了許多強大的選項(將任何網頁轉換為pdf,將任何html字符串轉換為pdf,html5 / css3 / javascript支持,頁眉和頁腳支持等),唯一的限制是它最多可以生成pdf文檔。5頁長。
.NET的免費HTML至Pdf轉換器–社區版功能:最多生成5頁pdf文檔,將任何網頁轉換為pdf,將任何原始html字符串轉換為pdf,設置pdf頁面設置(頁面大小,頁面方向,頁面邊距) ,在轉換過程中調整內容大小以適合pdf頁面,設置pdf文檔屬性,設置pdf查看器首選項,設置pdf安全性(密碼,權限),設置轉換延遲和網頁導航超時,自定義頁眉和頁腳,在頁眉中支持html和頁腳,自動和手動分頁符,在每個頁面上重復html表頭,支持@media類型屏幕和打印,支持內部和外部鏈接,基于html元素自動生成書簽,支持HTTP標頭,支持HTTP cookie,支持需要身份驗證的網頁,支持代理服務器,啟用/禁用javascript,修改顏色空間,多線程支持,HTML5 / CSS3支持,Web字體支持等等。
1、nuget 引用
Install-Package Select.HtmlToPdf
2、方法
using SelectPdf;
using System.Collections.Specialized;
using System.IO;
using System.Web;
namespace BQoolCommon.Helpers.File
{
public class WebToPdf
{
public WebToPdf()
{
//SelectPdf.GlobalProperties.LicenseKey = "your-license-key";
}
/// <summary>
/// 將 Html 轉成 PDF,並儲存成檔案
/// </summary>
/// <param name="html">html</param>
/// <param name="fileName">絕對路徑</param>
public void SaveToFileByHtml(string html, string fileName)
{
var doc = SetPdfDocument(html);
doc.Save(fileName);
}
/// <summary>
/// 傳入 Url 轉成 PDF,並儲存成檔案
/// </summary>
/// <param name="url">url</param>
/// <param name="fileName">絕對路徑</param>
/// <param name="httpCookies">Cookies</param>
public void SaveToFileByUrl(string url, string fileName, NameValueCollection httpCookies)
{
var doc = SetPdfDocument(url, httpCookies);
doc.Save(fileName);
}
/// <summary>
/// 將 Html 轉成 PDF,並輸出成 byte[] 格式
/// </summary>
/// <param name="html">html</param>
/// <returns></returns>
public byte[] GetFileByteByHtml(string html)
{
var doc = SetPdfDocument(html);
return doc.Save();
}
/// <summary>
/// 傳入 Url 轉成 PDF,並輸出成 byte[] 格式
/// </summary>
/// <param name="url">url</param>
/// <param name="httpCookies">Cookies</param>
/// <returns></returns>
public byte[] GetFileByteByUrl(string url, NameValueCollection httpCookies)
{
var doc = SetPdfDocument(url, httpCookies);
return doc.Save();
}
/// <summary>
/// 將 Html 轉成 PDF,並輸出成 Stream 格式
/// </summary>
/// <param name="html">html</param>
/// <returns></returns>
public Stream GetFileStreamByHtml(string html)
{
var doc = SetPdfDocument(html);
var pdfStream = new MemoryStream();
doc.Save(pdfStream);
pdfStream.Position = 0;
return pdfStream;
}
/// <summary>
/// 傳入 Url 轉成 PDF,並輸出成 Stream 格式
/// </summary>
/// <param name="html">html</param>
/// <returns></returns>
public Stream GetFileStreamByUrl(string url, NameValueCollection httpCookies)
{
var doc = SetPdfDocument(url, httpCookies);
var pdfStream = new MemoryStream();
doc.Save(pdfStream);
pdfStream.Position = 0;
return pdfStream;
}
private PdfDocument SetPdfDocument(string html)
{
var converter = new HtmlToPdf();
converter.Options.WebPageWidth = 1200;
html = HttpUtility.HtmlDecode(html);
return converter.ConvertHtmlString(html);
}
private PdfDocument SetPdfDocument(string url, NameValueCollection httpCookies)
{
var converter = new HtmlToPdf();
converter.Options.WebPageWidth = 1200;
if (httpCookies != && httpCookies.Count != 0)
{
converter.Options.HttpCookies.Add(httpCookies);
}
return converter.ConvertUrl(url);
}
}
}
3、調用
/// <summary>
/// 下載pdf
/// </summary>
public void Downpdf(string data)
{
var stream = new BQoolCommon.Helpers.File.WebToPdf().GetFileStreamByHtml(Gethtml(data));
Response.Clear();
//二進制流數據(如常見的文件下載)
Response.ContentType = "application/octet-stream";
//通知瀏覽器下載文件而不是打開
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("Profit and Loss Statement.pdf", System.Text.Encoding.UTF8));
var bytes = StreamToBytes(stream);
Response.BinaryWrite(bytes);
Response.Flush();
stream.Close();
stream.Dispose();
Response.End();
}
那么如何獲取指定頁面的html 呢 傳入對應的model 獲得指定動態的html
private string Gethtml(string data)
{
string str = "";
str = this.ControllerContext.RenderViewToString("ProfitDetails", data);
return str;
}
using BQoolCommon.Helpers.Format;
using Newtonsoft.Json;
using OrdersManager.Models.ViewModel.Report;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace OrdersManager.Web.Infrastructure
{
public static class HelperExtensions
{
public static string RenderViewToString(this ControllerContext context, string viewName, string data)
{
if (string.IsOrEmpty(viewName))
viewName = context.RouteData.GetRequiredString("action");
context.Controller.ViewData.Model = JsonConvert.DeserializeObject<ProfitDetailsmodel>(StringTools.Base64Decode(StringTools.Base64Decode(data)));
using (var sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(context, viewName);
var viewContext = new ViewContext(context,
viewResult.View,
context.Controller.ViewData,
context.Controller.TempData,
sw);
try
{
viewResult.View.Render(viewContext, sw);
}
catch (Exception ex)
{
throw;
}
return sw.GetStringBuilder().ToString();
}
}
}
}
https://www.nuget.org/packages/Select.HtmlToPdf/
*請認真填寫需求信息,我們會在24小時內與您取得聯系。