How to force a page break in HtmlRenderer.PdfSharp? - html-renderer

I'm using "HTML Renderer for PDF using PDFsharp" HtmlRenderer.PdfSharp (version 1.5.1-beta1). I'm trying to force a page break. But I can't get this to work. What I have now in my html is this:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Test</title>
<style>
div { page-break-inside: auto; }
</style>
</head>
<body style="margin:0; padding:0;" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<div>Page1</div>
<div>Page2</div>
</body>
</html>
Both div stay on the same page when I convert this html to PDF.
string contents = File.ReadAllText(#"C:\temp\test.html");
PdfDocument pdf = PdfGenerator.GeneratePdf(contents, PageSize.A4);
pdf.Save(#"C:\temp\pdfsharp.pdf");
How can I force the second div to a new page?

The only known solution to force page breaks is to split the html into parts, and generate a page for each html part. Solution from Grasher134 on GitHub: https://github.com/ArthurHub/HTML-Renderer/issues/49#issuecomment-251351431

Related

ReDoc cannot display a local OpenAPI definition file

ReDoc displays a remote openapi.json normally:
<?xml version="1.0" encoding="UTF-8"?><html>
<head>
<title>ReDoc - openapi.json</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<style>
body {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<redoc spec-url="https://petstore3.swagger.io/api/v3/openapi.json"/>
<script src="https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js">/**/</script>
</body>
</html>
But if I save openapi.json locally
<?xml version="1.0" encoding="UTF-8"?><html>
<head>
<title>ReDoc - openapi.json</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<style>
body {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<redoc spec-url="file:///tmp/openapi.json"/>
<script src="https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js">/**/</script>
</body>
</html>
it shows this error:
Something went wrong...
process is not defined
Stack trace
resolve#https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js:8:301803
resolveExternalRef#https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js:8:76499
4182/resolveDocument/<#https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js:8:77225
4182/r</<#https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js:8:74930
4182/r<#https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js:8:74675
resolveDocument#https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js:8:77178
3675/t.bundle/<#https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js:8:33256
3675/r</<#https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js:8:28947
3675/r<#https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js:8:28692
3675/t.bundle#https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js:8:33035
n#https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js:117:579
Mu/</n/e<#https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js:117:1021
Mu/</n/e<#https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js:117:837
n#https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js:117:1040
Mu/</<#https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js:117:1261
Mu/</<#https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js:117:1077
Mu/<#https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js:117:1280
Il#https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js:8:488153
53/t.unstable_runWithPriority#https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js:8:517125
Wo#https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js:8:428837
Rl#https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js:8:487616
4448/Cl/<#https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js:8:487527
D#https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js:8:516186
53/w.port1.onmessage#https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js:8:514909
ReDoc Version: 2.0.0
Commit: 5fb4daa
How to display a local OpenAPI definition file with ReDoc?
The easiest way is with Redocly CLI:
npx #redocly/cli preview-docs openapi.json
Source: https://redocly.com/docs/cli/commands/preview-docs/

style tag is ignored by uiwebview

I'm trying to give my html an css inline style. But this style gets ignored.
This is the HTML string that I'm using for webView.loadHTMLString(htmlBelow, baseURL: Bundle.main.bundleURL)
let fontsize = 16 //This is an dynamic variable
<!DOCTYPE html>
<html lang=\"en\">
<head>
<meta charset=\"UTF-8\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
<meta http-equiv=\"X-UA-Compatible\"content=\"ie=edge\">
<title>Document</title>
<style>
​html {
font-size:\(fontsize)px; */THIS DOESN'T WORK/*
}
</style>
<link rel=\"stylesheet\" href=\"\style.css\">
<script src=\"jquery-3.4.1.min.js\"></script>
<script src=\"script.js\"></script>
</head>
<body>
<div class=\ "sqr-tree-level\"> \(restOfHtml) </div>
</body>
</html>
I'm trying to set the fontsize dynamically.
When I change the font-size in my css file it works, but I want to set it dynamically. That's why I wanna do it like this.
This is how my document.head.innerHTML looks like
For future reference the fix here was to put the styling in to the <body> tag instead of in the head <style> tag.
<body style=\"font-size:\(fontsize)px;\">

How to use XPath in Ruby to get tag with a specific attribute?

For instance, I want to get the value inside this tag
<meta charset="euc-kr">
in this document:
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="euc-kr">
</Head>
</HTML>
How do I use XPath in Ruby to get any meta tag that has the attribute "charset"?
Ruby REXML library let's use XPath. Below example shows how to use xpath in your case.
require 'rexml/document'
xml = %{<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="euc-kr"></meta>
<link charset="en-us"></link>
</head>
</html>}
doc = REXML::Document.new xml
REXML::XPath.match(doc, '//*[#charset]')
Result:
=> [<meta charset='euc-kr'/>, <link charset='en-us'/>]

Grails Custom Tag Library not working in Main Layout

I have made a custom Tag library which is used to import all the needed JS and CSS files which are needed on all GSP pages. Now the problem is that if I try to use the TagLib on Main.gsp layout then the tag library doesnot work. But if I do the same on normal pages then it works perfectly fine.
I have even used a println to check on the console if the Tag library is being computed but nothing is getting printed. On the contrary if I use the same tagLib on the page then this println prints on console which proves it works there.
Is there any way to make it work on Main.gsp layout?
The code for taglib is-
def globalCSS = { attrs, body ->
out << '<!-- BEGIN GLOBAL MANDATORY STYLES -->'
out << '<link href="http://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700&subset=all" rel="stylesheet" type="text/css">'
out << '<link rel="stylesheet" type="text/css" href="'+g.resource(dir: 'assets/global/plugins/font-awesome/css', file: 'font-awesome.min.css', absolute: true)+'"/>'
out << '<link rel="stylesheet" type="text/css" href="'+g.resource(dir: 'assets/global/plugins/simple-line-icons', file: 'simple-line-icons.min.css', absolute: true)+'"/>'
out << '<link rel="stylesheet" type="text/css" href="'+g.resource(dir: 'assets/global/plugins/bootstrap/css', file: 'bootstrap.min.css', absolute: true)+'"/>'
out << '<link rel="stylesheet" type="text/css" href="'+g.resource(dir: 'assets/global/plugins/uniform/css', file: 'uniform.default.css', absolute: true)+'"/>'
out << '<!-- END GLOBAL MANDATORY STYLES -->'
println("Hello css");
}
The code for main.gsp is-
<!DOCTYPE html>
<!--[if lt IE 7 ]> <html lang="en" class="no-js ie6"> <![endif]-->
<!--[if IE 7 ]> <html lang="en" class="no-js ie7"> <![endif]-->
<!--[if IE 8 ]> <html lang="en" class="no-js ie8"> <![endif]-->
<!--[if IE 9 ]> <html lang="en" class="no-js ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html lang="en" class="no-js"><!--<![endif]-->
<head>
<myUI:globalCSS/>
<g:layoutHead/>
</head>
<body>
<g:layoutBody/>
</body>
</html>
It looks like you're reinventing functionality that is already provided by the resources/asset-pipeline plugins. Unless you have a good reason not to, I would suggest you use one of those instead, preferably asset-pipeline.

How to downcase an entire HTML document parsed with Nokogiri

I need to downcase all text in an HTML document that has been parsed with Nokogiri. Here my code:
agent = Mechanize.new
page = agent.get('http://www.example.com').parser.search('//*[translate(text(),"ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz") = *]').to_html
There is not error as such in the code; it executes without an error. If I go in and check a random tag in the document, however, the case is still the same as before. Is there another/better way to downcase all text in a document?
You could use traverse to downcase all text nodes:
require 'open-uri'
require 'nokogiri'
doc = Nokogiri::HTML(open("http://www.example.com/"))
doc.traverse do |node|
node.content = node.content.downcase if node.text?
end
puts doc.to_html
Output:
<!DOCTYPE html>
<html>
<head>
<title>example domain</title>
<meta charset="utf-8">
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
body { ... }
</style>
</head>
<body>
<div>
<h1>example domain</h1>
<p>this domain is established to be used for illustrative examples in documents. you may use this
domain in examples without prior coordination or asking for permission.</p>
<p>more information...</p>
</div>
</body>
</html>

Resources