Character encoding error when writing url in FPDF2How to get the ASCII value of a character?Twitter image encoding challengeWhat characters can be used for up/down triangle (arrow without stem) for display in HTML?Correct way to write line to file?What does the 'b' character do in front of a string literal?UnicodeEncodeError: 'ascii' codec can't encode character u'xa0' in position 20: ordinal not in range(128)Using unicode characters in Python's command line (Python 3.4.1.)Why is executing Java code in comments with certain Unicode characters allowed?Unicode writing fail: charmap can't encode characterWhy is the font not loading in pfpd.add_font()?

Overwrite file only if data

Sleeping solo in a double sleeping bag

Why is Boris Johnson visiting only Paris & Berlin if every member of the EU needs to agree on a withdrawal deal?

Can you be convicted for being a murderer twice?

How to persuade recruiters to send me the Job Description?

Defense against attacks using dictionaries

The teacher logged me in as administrator for doing a short task, is the whole system now compromised?

Taking out number of subarrays from an array which contains all the distinct elements of that array

Co-author responds to email by mistake cc'ing the EiC

Should my "average" PC be able to discern the potential of encountering a gelatinous cube from subtle clues?

Something in the TV

Have only girls been born for a long time in this village?

How to look up identical column names in two dataframes and combine the matched columns

Why doesn't the Falcon-9 first stage use three legs to land?

How to compare two different formulations of a problem?

What is "Wayfinder's Guide to Eberron"?

What is the hex versus octal timeline?

Most practical knots for hitching a line to an object while keeping the bitter end as tight as possible, without sag?

Is there such a thing as too inconvenient?

Are there nouns that change meaning based on gender?

Why would the US President need briefings on UFOs?

How can I support the recycling, but not the new production of aluminum?

What is the difference between a premise and an assumption in logic?

What professions would a medieval village with a population of 100 need?



Character encoding error when writing url in FPDF2


How to get the ASCII value of a character?Twitter image encoding challengeWhat characters can be used for up/down triangle (arrow without stem) for display in HTML?Correct way to write line to file?What does the 'b' character do in front of a string literal?UnicodeEncodeError: 'ascii' codec can't encode character u'xa0' in position 20: ordinal not in range(128)Using unicode characters in Python's command line (Python 3.4.1.)Why is executing Java code in comments with certain Unicode characters allowed?Unicode writing fail: charmap can't encode characterWhy is the font not loading in pfpd.add_font()?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I'm working in Python 3.7 and have the python port of FPDF 2.0.3 (https://pypi.org/project/fpdf2/) running. I'm working with a lot of unicode symbols, and some of them need to be a part of the URL. I can write them as text without problem, but I keep getting error messages when a unicode symbol becomes a part of my URL.



Tried to escape with html.escape(str), this didn't work
Rewritten the code to use write_html(html_as_str), this didn't work either



this is working code:



add = "Chronique d‘Égypte (CdE)" ##this is actually pulled in from a MYSQL query using PYMYSQL
from fpdf import FPDF
pdf = FPDF('P', 'mm', 'A4')
pdf.add_page()
pdf.add_font('base', '', r'C:FontsDejaVuSans.ttf', uni=True)
pdf.set_font('base', '',12)
pdf.write(5, add)
pdf.output("goodfile.pdf", "F")



With this script I get the string printed. However I want the string to be printed and be a part of a url like this:



add = "Chronique d‘Égypte (CdE)" ##this is actually pulled in from a MYSQL query using PYMYSQL
from fpdf import FPDF
pdf = FPDF('P', 'mm', 'A4')
pdf.add_page()
pdf.add_font('base', '', r'C:FontsDejaVuSans.ttf', uni=True)
pdf.set_font('base', '',12)
pdf.write(5, add, 'https://www.example.org/index.php?searchterm='+add)
pdf.output("goodfile.pdf", "F")



This second script fails with the following error message in my console:



Traceback (most recent call last):
File ".print.py", line 543, in <module>
pdf.output(Filename+'_CORPUS.pdf', 'F')
File "C:UsersmeAppDataLocalProgramsPythonPython37libsite-packagesfpdffpdf.py", line 1239, in output
buffer = self.buffer.encode("latin1")
UnicodeEncodeError: 'latin-1' codec can't encode character 'u2018' in position 1587960: ordinal not in range(256)



The error message itself is very confusing as the part that adds the string that causes the error happens around line 470; whereas the traceback only mentions line 543.



I expect the output to be a clickable link in my pdf, opening the default browser and going to the specified URL with the characters as they are in the PDF. I can't replace this character by a normal quote, as it give me other (in this case none) results on that site.



Also, could someone add a tag FPDF2 to help categorize this correctly?










share|improve this question



















  • 1





    The URL should probably be encoded somehow but we have no idea which encoding your server supports and expects. A reasonable guess would be to encode to UTF-8 and apply URL percent-encoding to the result; so "olé" maps to ol%C3%A9

    – tripleee
    Mar 27 at 15:47











  • Thanks Tripleee; that was indeed the issue. I had to import quote from urllib like: ` From urllib.parse import quote ` and then use quote round the string like: ` pdf.write(5, add, 'example.org/index.php?searchterm='+quote(add)) `

    – Clueless_captain
    Mar 28 at 8:41


















0















I'm working in Python 3.7 and have the python port of FPDF 2.0.3 (https://pypi.org/project/fpdf2/) running. I'm working with a lot of unicode symbols, and some of them need to be a part of the URL. I can write them as text without problem, but I keep getting error messages when a unicode symbol becomes a part of my URL.



Tried to escape with html.escape(str), this didn't work
Rewritten the code to use write_html(html_as_str), this didn't work either



this is working code:



add = "Chronique d‘Égypte (CdE)" ##this is actually pulled in from a MYSQL query using PYMYSQL
from fpdf import FPDF
pdf = FPDF('P', 'mm', 'A4')
pdf.add_page()
pdf.add_font('base', '', r'C:FontsDejaVuSans.ttf', uni=True)
pdf.set_font('base', '',12)
pdf.write(5, add)
pdf.output("goodfile.pdf", "F")



With this script I get the string printed. However I want the string to be printed and be a part of a url like this:



add = "Chronique d‘Égypte (CdE)" ##this is actually pulled in from a MYSQL query using PYMYSQL
from fpdf import FPDF
pdf = FPDF('P', 'mm', 'A4')
pdf.add_page()
pdf.add_font('base', '', r'C:FontsDejaVuSans.ttf', uni=True)
pdf.set_font('base', '',12)
pdf.write(5, add, 'https://www.example.org/index.php?searchterm='+add)
pdf.output("goodfile.pdf", "F")



This second script fails with the following error message in my console:



Traceback (most recent call last):
File ".print.py", line 543, in <module>
pdf.output(Filename+'_CORPUS.pdf', 'F')
File "C:UsersmeAppDataLocalProgramsPythonPython37libsite-packagesfpdffpdf.py", line 1239, in output
buffer = self.buffer.encode("latin1")
UnicodeEncodeError: 'latin-1' codec can't encode character 'u2018' in position 1587960: ordinal not in range(256)



The error message itself is very confusing as the part that adds the string that causes the error happens around line 470; whereas the traceback only mentions line 543.



I expect the output to be a clickable link in my pdf, opening the default browser and going to the specified URL with the characters as they are in the PDF. I can't replace this character by a normal quote, as it give me other (in this case none) results on that site.



Also, could someone add a tag FPDF2 to help categorize this correctly?










share|improve this question



















  • 1





    The URL should probably be encoded somehow but we have no idea which encoding your server supports and expects. A reasonable guess would be to encode to UTF-8 and apply URL percent-encoding to the result; so "olé" maps to ol%C3%A9

    – tripleee
    Mar 27 at 15:47











  • Thanks Tripleee; that was indeed the issue. I had to import quote from urllib like: ` From urllib.parse import quote ` and then use quote round the string like: ` pdf.write(5, add, 'example.org/index.php?searchterm='+quote(add)) `

    – Clueless_captain
    Mar 28 at 8:41














0












0








0








I'm working in Python 3.7 and have the python port of FPDF 2.0.3 (https://pypi.org/project/fpdf2/) running. I'm working with a lot of unicode symbols, and some of them need to be a part of the URL. I can write them as text without problem, but I keep getting error messages when a unicode symbol becomes a part of my URL.



Tried to escape with html.escape(str), this didn't work
Rewritten the code to use write_html(html_as_str), this didn't work either



this is working code:



add = "Chronique d‘Égypte (CdE)" ##this is actually pulled in from a MYSQL query using PYMYSQL
from fpdf import FPDF
pdf = FPDF('P', 'mm', 'A4')
pdf.add_page()
pdf.add_font('base', '', r'C:FontsDejaVuSans.ttf', uni=True)
pdf.set_font('base', '',12)
pdf.write(5, add)
pdf.output("goodfile.pdf", "F")



With this script I get the string printed. However I want the string to be printed and be a part of a url like this:



add = "Chronique d‘Égypte (CdE)" ##this is actually pulled in from a MYSQL query using PYMYSQL
from fpdf import FPDF
pdf = FPDF('P', 'mm', 'A4')
pdf.add_page()
pdf.add_font('base', '', r'C:FontsDejaVuSans.ttf', uni=True)
pdf.set_font('base', '',12)
pdf.write(5, add, 'https://www.example.org/index.php?searchterm='+add)
pdf.output("goodfile.pdf", "F")



This second script fails with the following error message in my console:



Traceback (most recent call last):
File ".print.py", line 543, in <module>
pdf.output(Filename+'_CORPUS.pdf', 'F')
File "C:UsersmeAppDataLocalProgramsPythonPython37libsite-packagesfpdffpdf.py", line 1239, in output
buffer = self.buffer.encode("latin1")
UnicodeEncodeError: 'latin-1' codec can't encode character 'u2018' in position 1587960: ordinal not in range(256)



The error message itself is very confusing as the part that adds the string that causes the error happens around line 470; whereas the traceback only mentions line 543.



I expect the output to be a clickable link in my pdf, opening the default browser and going to the specified URL with the characters as they are in the PDF. I can't replace this character by a normal quote, as it give me other (in this case none) results on that site.



Also, could someone add a tag FPDF2 to help categorize this correctly?










share|improve this question














I'm working in Python 3.7 and have the python port of FPDF 2.0.3 (https://pypi.org/project/fpdf2/) running. I'm working with a lot of unicode symbols, and some of them need to be a part of the URL. I can write them as text without problem, but I keep getting error messages when a unicode symbol becomes a part of my URL.



Tried to escape with html.escape(str), this didn't work
Rewritten the code to use write_html(html_as_str), this didn't work either



this is working code:



add = "Chronique d‘Égypte (CdE)" ##this is actually pulled in from a MYSQL query using PYMYSQL
from fpdf import FPDF
pdf = FPDF('P', 'mm', 'A4')
pdf.add_page()
pdf.add_font('base', '', r'C:FontsDejaVuSans.ttf', uni=True)
pdf.set_font('base', '',12)
pdf.write(5, add)
pdf.output("goodfile.pdf", "F")



With this script I get the string printed. However I want the string to be printed and be a part of a url like this:



add = "Chronique d‘Égypte (CdE)" ##this is actually pulled in from a MYSQL query using PYMYSQL
from fpdf import FPDF
pdf = FPDF('P', 'mm', 'A4')
pdf.add_page()
pdf.add_font('base', '', r'C:FontsDejaVuSans.ttf', uni=True)
pdf.set_font('base', '',12)
pdf.write(5, add, 'https://www.example.org/index.php?searchterm='+add)
pdf.output("goodfile.pdf", "F")



This second script fails with the following error message in my console:



Traceback (most recent call last):
File ".print.py", line 543, in <module>
pdf.output(Filename+'_CORPUS.pdf', 'F')
File "C:UsersmeAppDataLocalProgramsPythonPython37libsite-packagesfpdffpdf.py", line 1239, in output
buffer = self.buffer.encode("latin1")
UnicodeEncodeError: 'latin-1' codec can't encode character 'u2018' in position 1587960: ordinal not in range(256)



The error message itself is very confusing as the part that adds the string that causes the error happens around line 470; whereas the traceback only mentions line 543.



I expect the output to be a clickable link in my pdf, opening the default browser and going to the specified URL with the characters as they are in the PDF. I can't replace this character by a normal quote, as it give me other (in this case none) results on that site.



Also, could someone add a tag FPDF2 to help categorize this correctly?







python python-3.x pdf unicode






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 27 at 15:17









Clueless_captainClueless_captain

1389 bronze badges




1389 bronze badges










  • 1





    The URL should probably be encoded somehow but we have no idea which encoding your server supports and expects. A reasonable guess would be to encode to UTF-8 and apply URL percent-encoding to the result; so "olé" maps to ol%C3%A9

    – tripleee
    Mar 27 at 15:47











  • Thanks Tripleee; that was indeed the issue. I had to import quote from urllib like: ` From urllib.parse import quote ` and then use quote round the string like: ` pdf.write(5, add, 'example.org/index.php?searchterm='+quote(add)) `

    – Clueless_captain
    Mar 28 at 8:41













  • 1





    The URL should probably be encoded somehow but we have no idea which encoding your server supports and expects. A reasonable guess would be to encode to UTF-8 and apply URL percent-encoding to the result; so "olé" maps to ol%C3%A9

    – tripleee
    Mar 27 at 15:47











  • Thanks Tripleee; that was indeed the issue. I had to import quote from urllib like: ` From urllib.parse import quote ` and then use quote round the string like: ` pdf.write(5, add, 'example.org/index.php?searchterm='+quote(add)) `

    – Clueless_captain
    Mar 28 at 8:41








1




1





The URL should probably be encoded somehow but we have no idea which encoding your server supports and expects. A reasonable guess would be to encode to UTF-8 and apply URL percent-encoding to the result; so "olé" maps to ol%C3%A9

– tripleee
Mar 27 at 15:47





The URL should probably be encoded somehow but we have no idea which encoding your server supports and expects. A reasonable guess would be to encode to UTF-8 and apply URL percent-encoding to the result; so "olé" maps to ol%C3%A9

– tripleee
Mar 27 at 15:47













Thanks Tripleee; that was indeed the issue. I had to import quote from urllib like: ` From urllib.parse import quote ` and then use quote round the string like: ` pdf.write(5, add, 'example.org/index.php?searchterm='+quote(add)) `

– Clueless_captain
Mar 28 at 8:41






Thanks Tripleee; that was indeed the issue. I had to import quote from urllib like: ` From urllib.parse import quote ` and then use quote round the string like: ` pdf.write(5, add, 'example.org/index.php?searchterm='+quote(add)) `

– Clueless_captain
Mar 28 at 8:41













0






active

oldest

votes










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55380709%2fcharacter-encoding-error-when-writing-url-in-fpdf2%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes




Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.







Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55380709%2fcharacter-encoding-error-when-writing-url-in-fpdf2%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript